Skip to content
Theory · Ownership · lesson 2 of 8

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:

  1. Every value has an owner.
  2. At any moment a value has only one owner.
  3. 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:

Rust
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:

  1. let a = String::from("outer"); makes a the owner of the string outer. Its scope stretches to the last } of the function main.
  2. Inside the block, b becomes the owner of the string inner.
  3. Both variables are visible inside the block, and the program prints outer inner.
  4. At the block's }, b goes out of scope. By rule 3, Rust destroys the string inner and gives its unit back right here.
  5. a is still in scope, and the program prints outer.
  6. At the last }, a leaves too, and the unit with the string outer is 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:

Rust
fn main() {
    {
        let b = String::from("inner");
        println!("{b}");
    }
    println!("{b}"); // ✗
}
TEXT
error[E0425]: cannot find value `b` in this scope

The 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 let to 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.