Three rules of ownership
In the previous lesson, a unit at the storage facility had to be given back exactly once, and only after nobody used it anymore. Rust achieves this with three rules.
Picture each unit having one key, held by one person. That person is responsible for the unit: while they hold the key, the things inside are safe. When this person leaves the facility, the unit is freed, and nobody will open it again.
Three rules
The owner of a value is the variable that holds the value. This is the person with the key. There are three rules of ownership:
- Every value has an owner.
- At any moment a value has only one owner.
- When the owner goes out of scope, the value is destroyed, and its space on the heap is freed.
Interviewers like to ask this: "Name the three rules of ownership." The answer is exactly these three points. The usual follow-up asks what "goes out of scope" means.
Scope
A variable's scope is the part of the program where you can use it: from the line with let to the closing } of its block. A variable from a block disappears at its }. Now let's see what happens to a string on the heap at that moment:
fn main() {
let a = String::from("outer");
{
let b = String::from("inner");
println!("{a} {b}"); // outer inner
}
println!("{a}"); // outer
}Let's go step by step:
let a = String::from("outer");makesathe owner of the stringouter. Its scope stretches to the last}of the functionmain.- Inside the block,
bbecomes the owner of the stringinner. - Both variables are visible inside the block, and the program prints
outer inner. - At the block's
},bgoes out of scope. By rule 3, Rust destroys the stringinnerand gives its unit back right here. ais still in scope, and the program printsouter.- At the last
},aleaves too, and the unit with the stringouteris given back as well.
The compiler inserts the return
Nobody writes the return of a unit by hand. The compiler sees where the owner's scope ends and inserts the destruction of the value at that } itself. If several owners leave at the same }, they leave in reverse order: the last one declared goes first.
Numbers have owners too, but an i32 sits entirely on the stack tray. When its owner leaves, there is nothing to give back to the facility.
The rules remove the three mistakes with units like this:
- there is no forgotten return: the compiler inserts the return, not the programmer;
- there is no double return: only the owner gives the unit back, and there is one owner;
- you cannot open a unit that was given back: after the
}the owner's name is gone, and the compiler will not let you refer to it.
All of this is checked at compile time. Nothing of these checks remains in the finished program, so ownership does not slow it down.
Here the facility analogy falls short. At a real facility, you decide when to give the key back. In Rust, the compiler picks the moment: the value is destroyed at the } where the owner's scope ends.
Interviewers like to pin down what exactly ownership promises. Rust does not promise that memory leaks never happen: you can create one, sometimes even by accident. The guarantee is a different one: there are no double returns and no access to memory that was given back.
Common mistake
A variable is declared inside a block but used after it:
fn main() {
{
let b = String::from("inner");
println!("{b}");
}
println!("{b}"); // ✗
}error[E0425]: cannot find value `b` in this scopeThe program does not compile: the scope of b ended at the block's }, and the string is already destroyed. If you need the variable after the block too, declare it outside, before the block.
Key points
- Every value has one owner: the variable that holds it.
- A variable's scope runs from
letto the}of its block. - At that
}, Rust destroys the value itself and frees its space on the heap. - The compiler checks the rules, and the running program spends nothing on them.