Sub topic 4
Copy versus Clone side by side
This chapter assumes you know Rust structs, traits, and basic ownership and borrowing. You should also be able to read a compiler error. The previous chapters covered moves, Copy, and Clone one at a time. Here they are compared directly.
Two ways to get a second value
Both traits produce a second, independent value from an existing one. They differ in who triggers the duplication, what it costs, and whether you can customize it.
Copyduplicates implicitly. Any assignment, function argument, or return of aCopyvalue makes a bitwise copy. The original stays usable.Cloneduplicates explicitly. Nothing happens until you write.clone(). The code insideclonecan do anything, including allocating memory.
Without either trait, assignment is a move, and the original binding becomes unusable.
The comparison
| Aspect | `Copy` | `Clone` |
|---|---|---|
| Triggered by | The compiler, on every use by value | You, by calling `.clone()` |
| Visible in code | No | Yes |
| Implementation | Marker trait, no methods. Always a bitwise copy | `clone` method with arbitrary code |
| Cost | Always cheap, a memory copy of `size_of::<T>()` bytes | Anywhere from trivial to very expensive |
| Can run custom logic | No | Yes |
| Allowed with `Drop` | No | Yes |
| Relationship | `trait Copy: Clone`, so every `Copy` type is also `Clone` | Not every `Clone` type is `Copy` |
The supertrait relationship is why you always write #[derive(Clone, Copy)] together. Deriving Copy alone fails because Copy requires Clone.
Seeing the difference in code
#[derive(Debug, Clone, Copy)]
struct Id(u32);
#[derive(Debug, Clone)]
struct Name(String);
fn show_id(id: Id) {
println!("{:?}", id);
}
fn consume(name: Name) {
println!("{:?}", name);
}
fn main() {
let id = Id(7);
show_id(id);
show_id(id); // fine: each call receives a copy
let name = Name(String::from("Ada"));
consume(name.clone()); // explicit duplicate, heap data is copied
consume(name); // moves the original
// println!("{:?}", name); // error: borrow of moved value: `name`
}show_id(id) is called twice with no ceremony. For Name, the first call needs .clone() because consume takes ownership. The last call moves name, so using it afterward would not compile.
What `.clone()` does on a `Copy` type
Calling .clone() on a Copy type is legal. For derived implementations it produces the same result as a copy, so it only adds noise. Clippy flags it with clone_on_copy. A reader seeing .clone() should be able to assume that something non-trivial may happen.
Behavior you can and cannot customize
A Clone implementation can differ from a field-by-field duplicate. It might reset a counter, skip a cache field, or bump a reference count, as Rc does. You cannot do this with Copy. The compiler always performs a bitwise copy and never calls your code. Even if you write Clone by hand for a Copy type, the clone body should do the same thing as a plain copy. Otherwise a.clone() and let b = a; would disagree, which surprises readers.
Choosing at a glance
- If the value is a small plain-data type with no owned heap memory, consider
CopyandClone. - If duplication is meaningful but costly, implement only
Clone, so each duplicate is visible at the call site. - If duplication makes no sense, such as a file handle or a unique token, implement neither.
Later chapters cover the rules for deriving and the design guidelines in detail.
Exercise
A type Config contains a String and derives Clone. A teammate adds Copy to the derive list. Will it compile? What happens to this line if it did: let c2 = c1; println!("{:?}", c1);
Show answer
It will not compile. Copy requires every field to be Copy, and String owns heap memory, so it is not. Without Copy, the line let c2 = c1; moves c1, and the println! on c1 fails with "borrow of moved value". You would write let c2 = c1.clone(); to keep c1 usable.