5 Design patterns: Beyond the basics
This chapter covers
- Metaprogramming with macros
- Implementing the builder pattern in Rust
- Building fluent interfaces
- Observing the observer pattern
- Understanding the command pattern
- Exploring the newtype pattern
Chapters 2 and 3 introduced the core Rust building blocks: generics, traits, pattern matching, and functional programming features. In this chapter, we’re going to build on what we learned in those chapters by exploring those themes further and examining design patterns in Rust.
Using what we’ve learned, we can start to build more concrete patterns in a way that is consistent with Rust idioms. Although we won’t explore all the possible patterns, I’ll present carefully chosen examples that demonstrate the fundamentals needed to build nearly any design pattern in Rust.
If generics, traits, pattern matching, and closures are the raw ingredients of any design pattern, the patterns in this chapter represent archetypes of nearly any other pattern that combines those features. Before diving right into the patterns themselves, we’ll discuss macros, which aren’t patterns themselves but are often used in advanced design patterns.