In a previous post, I discussed the performance benefits of exposing references rather than cloning data outright. While refining my application logic recently, I realized that this principle applies broadly: eliminating unnecessary clones is one of the easiest ways to clean up general Rust code.
The Arc::clone Trap
Rust beginners often attempt to resolve stubborn borrow checker errors by simply cloning variables and moving them into closures or functions. While move is a fundamental idiom in Rust, excessive cloning should be avoided in production-grade code.
I’ve certainly been guilty of scattering Arc::clone across a codebase just to satisfy the compiler and push a feature across the finish line. The underlying motivation is often a casual approach to Arc.
Arc (Atomic Reference Counted pointer) is a foundational smart pointer for multithreaded Rust.…