chapter seven

7 Advanced Techniques, Part 1

 

This chapter covers

  • Using const generics
  • Applying traits to external crate types
  • Extending types with extension traits
  • Implementing blanket traits
  • Using marker traits to mark types with attributes
  • Tagging with structs
  • Providing access to internal data with reference objects

In the previous chapters, we introduced several Rust Advanced Techniques. In this chapter, we will expand on some of those themes and explore more advanced design patterns. These are patterns that are useful in many circumstances, but you’ll likely use them less frequently because they are more complex to implement, and often apply to specific scenarios that you may not encounter as often.

To use an analogy, the patterns discussed in the previous chapters describe standard tools you might find in every toolbox, which can be used for a wide variety of jobs: hammer, pliers, screwdriver, power drill. The patterns discussed in this chapter are for more specialized jobs, such as woodworking tools you would find in a wood shop: table saw, planer, lathe, band saw, and so on.

While these patterns are less useful for everyday Rust programming, it’s good to explore them so you understand how to use them when you do need them.

7.1 Const generics

7.2 Implementing traits for external crate types

7.2.1 Wrapper structs

7.2.2 Using Deref to unwrap a wrapped struct

7.3 Extension traits

7.4 Blanket traits

7.5 Marker traits

7.6 Struct tagging

7.7 Reference objects

7.8 Summary