What is the difference between Copy and Clone traits in Rust?
Understand the difference between Rust's Copy and Clone traits: implicit cheap copies versus explicit deep copies, with clear examples and common pitfalls.
Expected Interview Answer
Copy is an implicit, cheap bit-for-bit duplication that happens automatically on assignment for simple stack-only types, while Clone is an explicit, potentially expensive deep copy you invoke by calling .clone().
Copy is a marker trait for types whose values can be duplicated by simply copying their bits (integers, bools, chars, and structs made only of Copy fields). Assigning a Copy value doesn't move it — both bindings stay valid. Clone is a supertrait requirement of Copy but is more general: it can allocate and deep-copy heap data, and you must call .clone() explicitly. Types like String and Vec implement Clone but not Copy, because duplicating them requires heap allocation.
- Copy makes small value types ergonomic — no explicit call, no move errors
- Clone makes the cost of duplication explicit and visible in code
- Prevents accidental expensive copies of heap-owning types
- Copy types stay valid after assignment (no move)
- Clone can perform deep copies that Copy cannot
AI Mentor Explanation
Copy is like reading a teammate's score off the scoreboard — you glance and instantly have the same number, and the original still shows on the board unchanged, effortlessly. Clone is like hand-copying the entire match scorebook page by page: possible and sometimes necessary, but deliberate and costly. Rust makes the cheap glance automatic and forces you to explicitly ask for the laborious full transcription.
Step-by-Step Explanation
Step 1
Identify the type
Check whether the type is stack-only (integers, bool, char) or owns heap data (String, Vec).
Step 2
Derive Copy for simple types
Add #[derive(Copy, Clone)] to structs whose fields are all Copy — Copy requires Clone as a supertrait.
Step 3
Assignment behavior
Copy types duplicate implicitly on assignment and both bindings remain valid; non-Copy types move.
Step 4
Call clone explicitly
For heap-owning types call .clone() to make an explicit deep copy when you need an independent value.
Step 5
Mind the cost
Copy is always cheap; Clone may allocate, so make it visible and avoid it in hot paths.
What Interviewer Expects
- Copy is implicit; Clone is explicit via .clone()
- Copy is bit-wise; Clone can deep-copy heap data
- Copy requires Clone as a supertrait
- Examples: i32 is Copy, String is only Clone
- Copy types don't move on assignment
Common Mistakes
- Thinking String implements Copy
- Believing Clone is always cheap or shallow
- Forgetting that deriving Copy also requires deriving Clone
- Assuming assignment always moves — Copy types are duplicated instead
Best Answer (HR Friendly)
“Copy is an automatic, cheap duplicate that Rust does for small simple values without you asking, while Clone is a duplicate you request on purpose with .clone() and can be expensive because it copies data on the heap. In short, Copy is silent and small, Clone is explicit and possibly big.”
Code Example
// i32 is Copy: assignment duplicates, original stays valid
let a = 5;
let b = a; // implicit copy
println!("{a} {b}"); // both usable
// String is NOT Copy: assignment moves
let s1 = String::from("hi");
let s2 = s1.clone(); // explicit deep copy on the heap
println!("{s1} {s2}"); // both usable because we cloned
// Deriving Copy requires Clone too
#[derive(Copy, Clone)]
struct Point { x: i32, y: i32 }Follow-up Questions
- Why can't String implement Copy?
- Why does Copy require Clone as a supertrait?
- What happens to the original value after assigning a non-Copy type?
- When would deriving Copy be a bad idea for performance or semantics?
MCQ Practice
1. Which type can implement Copy?
i32 is stack-only and duplicable bit-for-bit, so it is Copy. String, Vec, and Box own heap data and are only Clone.
2. What is required to derive Copy on a struct?
Copy is a subtrait of Clone, so any Copy type must also be Clone; typically you derive both together.
Flash Cards
Copy vs Clone in one line — Copy is implicit cheap bit-copy on assignment; Clone is explicit and can deep-copy heap data via .clone().
Does String implement Copy? — No — it owns heap data, so it implements only Clone, not Copy.
Relationship between the traits — Copy is a supertrait relationship: every Copy type must also implement Clone.
What happens on assigning a Copy type? — The value is duplicated and both the original and new binding remain valid (no move).