Ownership and moves.
Learn why one owner is responsible for a value and what assignment can transfer.
The idea behind the syntax.
Ownership answers a concrete question: which binding is responsible for a value when its lifetime ends? In safe Rust, each owned value has one owner at a time. When that owner leaves scope, Rust runs cleanup automatically. Moving an owned value to another binding transfers that responsibility; the old binding can no longer use it. The compiler rejects a use after move because the same resource must not be cleaned up twice.
Simple types such as i32 are Copy: assignment duplicates their bits and both bindings remain valid. A String owns heap storage and is normally moved on assignment. clone explicitly duplicates its owned data. This is a cost distinction, not a judgement about which style is morally better. A move is often cheap; a clone can allocate and copy. Read a function signature to see whether it takes ownership, borrows, or returns a value.
What Rust is checking.
Stack and heap are useful as a rough picture, but ownership is a language rule rather than a direct map of memory locations. A String value includes metadata such as length and capacity and refers to bytes in heap storage. Moving the String changes which binding may use that allocation; it does not need to copy every byte. At scope end, the active owner drops the allocation.
The same reasoning applies to a function call. Passing a String by value moves it into the parameter unless the code deliberately clones it. Returning it can transfer ownership back to the caller. Borrowing, explored next, lets a function inspect or temporarily modify a value without taking over its cleanup responsibility. The aim is to make resource lifetime and mutation legible in the API.
See the rule in context.
Each example isolates one part of the model. Follow the bindings, types, ownership transfers, and possible outcomes line by line.
Ownership & moves
A value has an owner responsible for dropping it when its lifetime ends. Assigning a String to another binding transfers ownership rather than duplicating its heap allocation. The original binding is then unavailable. Passing an owned value into a function can transfer ownership in the same way; returning it can transfer it back. This makes cleanup predictable without requiring a garbage collector. Distinguish the owned String from an &str reference, which temporarily views text owned elsewhere.
fn label(mut name: String) -> String {
name.push_str(" / ready");
name
}
fn main() {
let first = String::from("engine");
let second = first;
let result = label(second);
println!("{result}");
}engine / ready
- The first-to-second assignment moves the String.
- label takes ownership and may mutate its local binding.
- The returned String becomes owned by result and is dropped at the end of main.
The boundary: A moved binding cannot be used even if you know the heap bytes still exist. The compiler follows ownership, not intuition about addresses.
Copy, Clone & cost
Small scalar types such as i32 implement Copy: assigning them leaves both values usable. A String does not implement Copy because it owns a heap buffer. clone explicitly creates another owned String and copies the text. Clone has type-specific behavior: cloning an Arc increments a reference count rather than deeply copying its payload. Read the type and its API before assuming what duplication costs. Borrow when you only need temporary access; clone when independent ownership is actually part of the design.
fn main() {
let a = 7;
let b = a;
let first = String::from("Rust");
let mut second = first.clone();
second.push('!');
println!("{a} {b}: {first} / {second}");
}7 7: Rust / Rust!
- The integer assignment copies.
- String::clone creates independently owned text.
- Changing second does not change first.
The boundary: Cloning to fix every borrowing error can hide an unclear ownership model and add unnecessary allocations.
Keep this model.
For every non-Copy value, ask who owns it before and after a line. That question explains moves, cleanup, and why a later use can fail to compile.