-
Book Overview & Buying
-
Table Of Contents
Design Patterns and Best Practices in Rust
By :
In this chapter, we will explore how traditional creational design patterns translate to Rust, and how Rust's unique features often provide better alternatives. We'll examine which patterns need adapting and which might be unnecessary due to the language's built-in features that serve the same purpose more effectively.
First, we'll look at Factory Method and how Rust's functions-as-constructors convention (see the Rust API Guidelines, "Constructors are static inherent methods (C-CTOR)": https://rust-lang.github.io/api-guidelines/predictability.html) provides a natural way to implement it. Then, we'll explore when Abstract Factory is helpful in Rust and when it's not needed. In many cases, Rust's trait system and generics can accomplish what Abstract Factory does in object-oriented languages without the same complexity. We'll see cases where simple trait bounds often suffice, and when...