Ownership & Borrowing · 16 min · free lesson

Lifetime Intuition

Lifetimes describe relationships between references, not timers.

Core idea

A lifetime annotation relates borrows; it never extends one.

Coming from C? Dangling Pointers and Lifetimes covers this side by side.

A reference timeline begins after its value and ends before the value is dropped.
A borrowed reference cannot be used beyond its source value.

Understand it

An annotation does not change how long anything lives. It tells the compiler how input and output borrows are connected, so it can prove a returned reference never outlives the data behind it. Most of the time inference handles this and you write nothing at all.

A lifetime is a relationship the compiler uses to prove a reference is valid when it is used. It is not a timer and an annotation cannot keep memory alive. In longest, both input references are related to the returned reference through 'a. The caller must use the result only while the selected input could still be valid. This is conservative: at compile time the function could return either argument, so the result is limited by the shorter applicable borrow.

Trace a call where one string lives for the whole function and another is created in an inner block. You may compare them and print the chosen slice inside that block. You cannot keep the returned reference for use after the inner string has gone away, because it might point into that string. Moving a lifetime annotation to a different letter would not change the data's scope. The remedy is to shorten the use of the result, return an owned String, or change which data the result can borrow from.

Most functions do not need written lifetime parameters. A function with one borrowed input and a borrowed output often has an obvious relationship, and Rust applies elision rules. Add annotations when multiple inputs make the source of the output ambiguous or when a struct stores a reference. In an error message, identify the owner of each referenced value and the last point at which it is alive. That timeline is more useful than trying random apostrophe labels until the code compiles.

Read the code

fn longest<'a>(
    left: &'a str,
    right: &'a str,
) -> &'a str {
    if left.len() > right.len() {
        left
    } else {
        right
    }
}

One name ties the result to both inputs.

Practice starter

fn pick(a: &str, b: &str) -> &str {
    if a.len() > b.len() { a } else { b }
}

Practise this code in Playground ↗

Try it

Describe which input borrow a returned reference may depend on.

What success looks like: You can explain why pick needs one lifetime name on both inputs and the result.

Check your understanding

Why does that need an annotation?

Reveal the answer
  1. Two inputs mean the compiler cannot tell which one the result borrows from
  2. A &str return always needs an annotation
  3. Every function that borrows must annotate its lifetimes

With one input reference it is inferred; with two the relationship must be stated.

Go deeper

Common misconception: “Writing 'a makes the borrowed strings live longer.”

'a only says the result is tied to both inputs, and it extends nothing. pick needs that statement because with two inputs the compiler cannot tell which one the result borrows from.

  1. Link the references: longest puts 'a on left, right, and the return type, so the result counts as borrowing from both inputs.
  2. Predict before checking: Call longest with one String from an outer scope and one from an inner block. Predict whether the result can be printed inside that block, and after it.
  3. Compare with evidence: Compile both uses: inside the block is accepted, and after the block E0597 says the inner string does not live long enough.

Transfer challenge: Annotate pick the way longest is annotated. Then make its body always return a, drop 'a from b, and check that the result can now outlive the string passed as b.

Connections

  • Drop & Scope: Deterministic cleanup without a GC
  • Generics: Reusable types and functions
Learn this lesson with AI

Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.

I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools.
Lesson: Lifetime Intuition (courseId "ownership", lessonId "lifetimes-intro")
Link: https://crabrix.com/learn/ownership/lifetimes-intro/
Goal: A lifetime annotation relates borrows; it never extends one.
Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=ownership/lifetimes-intro) and I will write the code there.
Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.

In Claude Code, Codex or Cursor, paste it into a chat with Crabrix Academy connected. On a phone, the Claude and ChatGPT apps open the same links.