Ownership
A program allocates memory on the heap, and someone has to free it — exactly once, and only when it is no longer needed. In C the programmer does it by calling free, and the bugs here are textbook ones: use-after-free, double free, and a leak, when memory is never freed at all. Java, Go and Python hand the job to a garbage collector: it removes the first two bugs, but it costs CPU and memory while the program runs, and the moment of freeing is not known in advance.
Rust takes a third path — ownership. Every value has exactly one owner, and when the owner goes out of scope, the value is dropped: the compiler itself inserts the destructor call in the right place. Assigning a String to another variable or passing it to a function does not copy the text but moves ownership: only the string's header is copied, the source variable becomes unusable, and an attempt to use it gives error[E0382]: borrow of moved value: `s` . Simple values like i32 implement Copy and are copied whole, while a deep copy of a string is made explicitly, with .clone(). All the checks happen at compile time and cost nothing while the program runs. Ownership does not rule out leaks, though: an Rc cycle and std::mem::forget are safe code. This topic walks ownership layer by layer — from the stack and the heap to passing ownership through functions.
Topic map
- Stack and Heap — the stack holds values of known size and cleans itself up, the heap holds data of arbitrary size that someone must free; a
String's header (pointer, length, capacity) lives on the stack, its bytes on the heap. - The Three Ownership Rules — a value has one owner, and when the owner goes out of scope the value is dropped; which C bugs this rules out and why leaks remain possible.
- Move Semantics —
let s2 = s1;copies the header and makess1unusable (E0382); you cannot take a value out of aVecby index (E0507);mem::take,mem::replaceandmem::swap. - Copy and Clone —
Copyis an implicit bitwise copy for types with noDropand no heap data;Cloneis an explicit and possibly deep copy; errors E0204 and E0184. - Drop and Scope — locals are dropped in reverse declaration order, struct fields in declaration order;
std::mem::dropand why.drop()cannot be called (E0040). - Ownership and Functions — an argument moves into the parameter, a return hands ownership to the caller; the "take and give back" pattern; partially moving a field out of a struct.
Common mistakes and traps
| Mistake | Consequence |
|---|---|
Using a String after let t = s; or after passing it to a function | error[E0382]: borrow of moved value: `s` : ownership went to another variable |
Taking a string out of a Vec<String> with let x = v[0]; | error[E0507]: cannot move out of index of `Vec<String>` ; you need &v[0], clone(), remove or mem::take |
Sprinkling .clone() to "calm down" the compiler | An extra allocation for every copy; a reference is usually enough |
Declaring Copy on a type with a String field or its own Drop | Errors E0204 and E0184: a bitwise copy would lead to a double free |
Calling the destructor by hand: x.drop() | error[E0040]: explicit use of destructor method; the right way is drop(x) |
| Expecting locals to be dropped in declaration order | The order is reversed; struct fields, meanwhile, are dropped in declaration order |
| Claiming that ownership rules out memory leaks | Wrong: Rc cycles, mem::forget and Box::leak are safe code, and a leak is not undefined behavior |
Why it matters in interviews
Ownership is the main question of the Rust junior section and the foundation for borrowing, lifetimes and threads: someone who does not understand moves cannot explain a single compiler error further on. Interviewers ask both theory — the three rules and why they exist — and practice: "why does this code not compile, and how do you fix it".
What interviewers usually check:
- The three ownership rules and which C and C++ bugs they rule out without a garbage collector.
- Where a
String's header and its bytes live, and what exactly a move copies. - Why after
let b = a;the variableais still usable for ani32but not for aString. - How
Copydiffers fromClone, and why a type withDropor with heap data cannot beCopy. - In what order values are dropped, why
std::mem::dropexists, and why you cannot call.drop(). - Whether safe Rust can leak memory.