
Understand it
Types whose fields are all Copy and which do not implement Drop can implement Copy, so assigning them leaves the original usable. An owning String or Vec is not Copy; duplicating those owners requires an explicit operation such as .clone() — the cost is visible in the source rather than hidden in an assignment.
Copy means a value can be duplicated by copying its bits, with no visible method call. Primitive integers and many small plain-data structs qualify. A type with a destructor cannot be Copy, because two implicit copies would both try to perform cleanup. String owns a heap allocation and therefore moves on ordinary assignment. Clone is different: it is an explicit operation, and its cost depends on the type. Cloning a String duplicates its bytes into a new buffer.
In the sample, a and b are separate integers after let b = a, so both print. The string expression uses s.clone() to create t; after that, both own their own text. If you change the line to let t = s, only t remains usable. The output may look identical when you do clone, but the memory and performance behavior are not. That is why Rust makes the expensive path visible.
Do not add .clone() automatically whenever the compiler reports a move. Ask whether the next operation only needs &T, whether ownership can be transferred to the right place, or whether two independent values are genuinely required. Try deriving Copy for a struct containing only u32 fields, then add a String field and read the new error. This reveals the trait requirement through a concrete type. Use cloning when independent ownership is part of the design, and borrowing when the other code only needs temporary access.
An Arc<T> clone illustrates another nuance: it usually increments a reference count rather than duplicating all the data inside. The method name means an explicit duplicate of the value’s ownership handle, not necessarily a deep copy of every reachable byte.
Copy is a semantic contract, not a promise that the value is small or cheap: a large array of Copy elements is still Copy. Clone need not deep-copy data; cloning an Arc<T> shares the same allocation and increments its reference count. Decide whether the API needs another owner or independent data.
Read the code
let a = 5; // i32 is Copy
let b = a;
println!("{a} {b}"); // both still usable
let s = String::from("crab");
let t = s.clone(); // explicit deep copy
println!("{s} {t}");The integer copies; the String would have moved without .clone().
Practice starter
let name = String::from("ada");
let alias = name;
println!("{name}");Try it
Return two independent owned strings containing the borrowed input. The caller keeps its original text. In the Crabrix app, open the compiler lab and complete the supplied function in solution.rs. The reading question is a separate exercise.
What success looks like: The local compiler accepts the changed solution and the separate verification harness finishes all normal and boundary cases. Printing a success message alone does not complete this lab.
Check your understanding
How do you make that compile without changing the println?
Reveal the answer
- Add mut to name
- Use name.clone() for alias
- Wrap it in a Box
Cloning creates a second owned String, so the original binding stays valid.
Go deeper
Common misconception: “Assignment copies a String the same way it copies an i32.”
i32 is Copy, so let b = a leaves both usable. String owns a heap buffer and is not Copy, so let alias = name moves it; name.clone() is how you ask for a second buffer.
- Sort the types: In the example,
ais ani32andsis aString.let b = a;copies the bits, whilelet t = s.clone();allocates a second buffer on purpose. - Predict before checking: The practice code writes
let alias = name;with no.clone(). Predict whetherprintln!("{name}");compiles. - Compare with evidence: Compile it: E0382 reports that
namemoved intoalias. Change the line tolet alias = name.clone();andadaprints.
Transfer challenge: Derive Clone and Copy on a struct with two u32 fields and assign it twice. Then add a String field and read the error that rejects the Copy derive.
Connections
- Slices: Borrowed views into data
- Drop & Scope: Deterministic cleanup without a GC
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Copy & Clone (courseId "ownership", lessonId "copy-clone") Link: https://crabrix.com/learn/ownership/copy-clone/ Goal: Copy permits implicit bitwise duplication; Clone is explicit and has a type-specific cost. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=ownership/copy-clone) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.