-
Book Overview & Buying
-
Table Of Contents
Design Patterns and Best Practices in Rust
By :
In this chapter, we discussed the anti-pattern of trying to apply OO design in Rust. We examined how, though it may seem natural to use familiar OO patterns, and may even work to some extent, these patterns are not a path to successfully designing and building systems and applications in Rust.
First, we explored the reasons why while Rust can seem so much like an OO language, it really isn't, and mistaking it for one can lead to bad designs and messy code.
Then we used our Bad Calculator project to try out using Rust traits as a stand-in for interfaces or pure abstract classes, and how this appeared at first to be a workable solution, but became less and less so as we built the application.
We next tried using the Deref trait to try to recreate the benefits of inheritance in our code, but discovered that this trick doesn't pay off.
After that, we tried using generics to simulate a base class, and found that, while it seemed to work, it didn't...