Ownership
Every value in Rust has one owner. When the owner goes out of scope, the value is dropped. Assignment moves the value unless the type implements Copy.
Detailed Explanation
Ownership is the rule that replaces a garbage collector for most Rust programs. A String has one owner. let a = String::from("ada"); let b = a; moves the string to b. a is done. Using a after that is a compile error, not a use-after-free. When b goes out of scope, Drop frees the buffer.
The rule has three parts. Each value has an owner. There is only one owner at a time. When the owner goes out of scope, the value is dropped. Functions participate: fn take(s: String) becomes the owner. fn peek(s: &String) does not. Return values move ownership out.
Copy is the exception. i32 is Copy. let b = a copies the bits and both names remain valid. String is not Copy because a bitwise copy would create two owners of one buffer. Clone is the explicit deep copy: a.clone() allocates a second buffer. You opt in to the cost.
Ownership is why Rust can be memory-safe without a GC pause. It is also why the first weeks feel like an argument with the compiler. The compiler is tracking a fact C leaves implicit: who frees this, and when. Cycles of ownership cannot be expressed with Box alone; you need Rc/Weak or a redesign.
When you move: large owned values, transferring a resource. When you clone: you truly need two owned copies. When you borrow: you only need to read or briefly write. Prefer borrow, then move, then clone.
Partial moves are allowed: let token = session.token moves one field and leaves the rest of session usable until you touch token again through session. After a partial move you cannot pass session as a whole. This is why Option::take exists on fields you need to move out of a still-live struct. Put the field in Option if you must empty it and keep the parent.
Code Examples
fn take(name: String) {
println!("{name}");
}
fn main() {
let a = String::from("ada");
take(a);
// println!("{a}"); // would not compile: a was moved
let n = 7;
let m = n; // Copy
println!("{n} {m}"); // prints: 7 7
}Common Gotchas
Use after move is a compile error
The fix is a borrow, a clone, or restructuring so the owner stays where you need it.
`clone` everywhere compiles and is slow
A `clone` that silences the checker is often a design smell. Try `&` first.
`Copy` is memcpy, not `Clone`
A 4 KiB `Copy` array copies 4 KiB on every assignment. That can be more expensive than moving a `Vec`.
Returning a value moves it out
You do not need `clone` to return a local `String` you no longer need.