2 Basic building blocks


This chapter covers

  • Exploring the core Rust idioms and patterns
  • Diving into Rust generics
  • Exploring traits
  • Combining generics and traits
  • Deriving traits automatically

In this chapter, we’ll introduce and discuss some of the most important abstractions and features in Rust. Nearly all design patterns will build atop of these core building blocks, thus it’s necessary to review them before diving deeper into other patterns. For some readers, this chapter may appear on the surface to be a review of language basics; however, we’re setting up for more advanced topics, so I recommend you don’t skip this. We will begin by discussing generics and traits in Rust, leading us to more advanced topics. Traits and generics are the core building blocks for nearly any design pattern in Rust, along with Rust’s pattern matching and functional features (which we’ll discuss in the next chapter). These constitute the "meat and potatoes" of the language.

2.1 Generics

2.1.1 Basics of generics

2.1.2 Exploring Option<T>

2.1.3 Generic parameter trait bounds

2.2 Traits

2.2.1 What’s in a trait?

2.2.2 Understanding traits by examining object-oriented code

2.2.3 Combining generics and traits

2.2.4 Deriving traits automatically

2.2.5 Trait objects

2.3 Summary