-
Book Overview & Buying
-
Table Of Contents
Design Patterns and Best Practices in Rust
By :
In this chapter, we will discuss more common anti-patterns in Rust, focusing on the overuse of cloning and smart pointers in an effort to avoid dealing with ownership and borrowing. We'll see how developers often reach for quick fixes when encountering borrow checker errors, and we will discuss why these approaches typically lead to inefficient, messy, and potentially buggy code.
First, we'll explore how developers try to avoid thinking about ownership and borrowing altogether, leading to designs that fight against the way Rust is intended to be used. Then, we'll examine the temptation to solve every ownership conflict with clone(), and why this creates both performance and correctness issues. Finally, we'll look at the misuse of reference counting and interior mutability (via Rc<RefCell<>>) as a misguided way to circumvent the borrow checker, and look at situations where that is the correct solution...