Rust's Copy vs Clone: What Rspack's Source Code Teaches About Moves and Copies
Choosing between Copy and Clone shapes both borrow-checker success and runtime cost. Rspack's pattern of keeping lightweight identifiers copyable and heavy graph nodes explicitly cloneable gives a concrete template for Rust type design.
Rspack's source code draws a clear line between Copy and Clone semantics. The module identifier type derives both Clone and Copy, so it can be passed by value and still be used later. The ModuleGraphModule type, which holds collections and strings, only derives Clone, so every duplication is an explicit .clone() call. This separation is not random; it follows the ownership rules of Rust.
Copy does not mean zero cost. The article tightens that common assumption: Copy allows implicit bitwise copying of the value representation, but the actual cost depends on the type and compiler optimizations. A large array or a string-interning type like Ustr may still involve data movement or allocation. Conversely, a Copy value might get optimized away entirely.
The practical usage appears in plugin code where .copied() on an iterator turns references into owned values without deep cloning. Explicit .clone() appears when a task needs a mutable working copy of an artifact entry. The takeaway is to read the type and its fields before judging whether an operation moves, copies, or allocates.
Copy does not guarantee zero cost: the article corrects the initial note, showing that machine instructions can be optimized away or require data movement.
Separating lightweight keys (Copy) from heavyweight owning nodes (Clone-only) is a type-design decision that propagates through the entire codebase, making the cost of each operation visible.
The .copied() call shows how Copy enables ergonomic collection from iterator references without triggering deep clones, a pattern that reduces accidental allocation.
The explicit .clone() in the plugin demonstrates that the goal is often ownership transfer for mutation, not mere duplication; it forces the developer to acknowledge the cost at the call site.
The article's stance that a type's representation, not just the Copy marker, determines cost challenges the common assumption that Copy is always free.
Reading Rust source code effectively requires checking whether a type is Copy or Clone before reasoning about assignments and dereferences, a habit that prevents subtle borrow errors and performance surprises.