跪拜 Guibai
← All articles
Frontend

Rust's Copy vs Clone: What Rspack's Source Code Teaches About Moves and Copies

By Amos_zz ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

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.

Summary

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.

Takeaways
— Identifier derives both Clone and Copy, allowing implicit copying without consuming the original.
— ModuleGraphModule only derives Clone, so assignments and passes move it, and duplication requires explicit .clone().
— Copy requires the type to also implement Clone, and all fields must satisfy Copy constraints.
— Types that implement Drop cannot implement Copy, because implicit bitwise copying would break resource release guarantees.
— The .copied() iterator adapter converts &T to T, but only for Copy types; it is used to collect ModuleIdentifier from modules_keys() without cloning module objects.
— Shared references &T are Copy; mutable references &mut T are not.
— Clone borrows self and returns a new value; it never automatically happens on assignment or pass-by-value.
— Clone does not guarantee deep copy: String and Vec clone contents, but Arc only increments a shared ownership count.
— Explicit cloning in Rspack's plugin gives a task an owned ExportsInfoData that can be mutated without holding a mutable borrow of the artifact.
— Inspect the type and its fields to judge cost: `let b = a` may move or copy, and `.clone()` may copy content or just share ownership.
— The minimal example compiles with rustc 1.95.0 and outputs id=7:7:7, original=7:entry, clone=7:entry copy, shared=true.
— Compiling the wrong examples yields E0382 for using a moved String and E0204 for deriving Copy on a struct with a String field.
Conclusions

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.

Concepts & terms
Copy trait
A Rust trait that enables implicit bitwise copying of a value. Types that implement Copy can be used after being moved or passed by value, because the original remains valid. Copy is a stronger commitment than Clone and requires all fields to be Copy.
Clone trait
A Rust trait that provides an explicit clone method, typically producing a new value that may share internal data (e.g., Arc) or deeply copy it (e.g., String). Cloning is never automatic and its cost depends on the type's implementation.
Ustr
A string interning type used in Rspack. It represents a cached, unique string reference, allowing fast comparisons and hashing. In Identifier, Ustr is the inner field, and because Ustr is Copy, Identifier can derive Copy.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗