Skip to content
Free public beta until December 4, 2026: every course is open, no card needed. After that, the first chapter of each course stays free and full access is a one-time $39 ($19 for up to 100 founding members chosen for helpful feedback). Your progress is kept. How the beta works

Recipe

Find the line that frees each value

Use when: a program makes heap values such as Strings, and you need to say on which line each buffer goes back

Goal: for each owner, the closing brace that drops its value, and the order of the drops at a shared brace

Taught in The ownership rules · a later section extends this recipe with more cases

  1. 1

    Write down each variable, made by a let, that holds a heap value. A tuple is one owner for everything inside it.

    Example

    In the city and band program, the owners are city and band. Each one holds one String.

    Why: rule 3 is about owners, so the owners are what you track.

  2. 2

    For each owner, find its let. Then find the } that closes the block that let sits in.

    Example

    band is made inside the inner block, so its brace is the } on line 6. city is made in main's body, so its brace is on line 8.

    • the let is inside an inner block, such as an if body or a bare { } — the brace is that block's }.
    • the let is right in the function's body — the brace is the function's last }.
    • Not yet: the value is handed to another variable or into a function — covered in Predicting moves

    Why: the owner goes out of scope at that brace, and rule 3 drops its value there.

  3. 3

    When two or more owners end at the same }, they are dropped in reverse order of their lets. The last one made goes first.

    Example

    In a main with let a = ...; and, further down, let c = ...;, both end at main's }. c is dropped first, then a.

    Why: Rust cleans up a block the way the stack (Chapter 6) works: newest first.

  4. 4

    When an owner is dropped, every value it holds is dropped with it.

    Example

    let menu = (String::from("tea"), String::from("cake")); has one owner and two buffers. Both buffers are freed at main's }, when menu is dropped.

    Why: ownership forms a tree, and dropping the top of it drops everything underneath.

Common mistakes

  • Counting variables instead of buffers. Count the String::from calls to count buffers.
  • Picking the function's last brace for a let that sits inside an inner block.
  • Dropping owners at a shared brace in the order they were written. It runs the other way.
  • Thinking you must free the Strings inside a tuple yourself.