-
Book Overview & Buying
-
Table Of Contents
Design Patterns and Best Practices in Rust
By :
We've tried some ways to model class hierarchies and objects in Rust and found that they cause more issues than they solve and produce awkward code. However, Rust is a flexible language, with many features that allow many ways to accomplish things. Maybe we can find a different way to model our class hierarchies.
Generic types in Rust, when properly used, provide an extremely expressive and effective tool for designing and architecting systems and applications. They are at the core of many of the most useful patterns and techniques, and they make it possible to create robust and flexible code that is very compact and readable. Can they help us build something like a class hierarchy?
Let's try this with our calculator. In a traditional OO approach, we might model operations with a class hierarchy:

Figure 2.2: Operation class hierarchy
The Operation base class would have common fields (such as precedence) and an...