Core Rust Questions · 16 min · free lesson

Lifetime Reasoning

Lifetimes are about relationships, and saying so is the answer.

Core idea

A lifetime constrains borrows; it never keeps anything alive.

Left and right input borrows overlap and feed a choose step; the returned reference spans only their overlap.
A lifetime relates a reference to the data it borrows.

Understand it

The common misconception is that a lifetime controls how long a value lives. It does not: it is a compile-time claim about which borrows outlive which. Annotations appear only where inference cannot decide, which is why most code has none.

A lifetime annotation relates references; it does not keep a value alive by itself. Consider a function returning one of two borrowed strings. The output reference must not outlive the input it came from. When the compiler cannot infer which input supplies the result, an annotation such as 'a names their relationship. At the call site, the borrow remains valid only while the underlying owner lives. No annotation can turn a reference to a local temporary into a valid long-lived return value.

Explain this with a failed example: create a String inside a function and return &str pointing into it. The string is dropped when the function ends, so the reference would dangle. Return the owned String instead, or have the caller provide storage that lives long enough. This makes the reason visible without treating lifetimes as magical syntax.

In an interview, start with who owns each value, which reference is returned, and how long each owner is available. Only then write the annotation. Most functions need no explicit lifetime syntax because the compiler can infer the relationship from simple signatures and uses. If two inputs have different lifetimes, do not force them to share a name unless the return contract truly depends on both. A good explanation says what guarantee the signature gives to callers and what it cannot guarantee.

If the interviewer asks for the longest lifetime, resist assigning a duration in seconds or scopes by guesswork. Show the returned reference depends on the shortest relevant input borrow. The caller can use it only while the chosen owner remains alive. That is the contract annotations communicate.

Interview follow-up: Rewrite the function to return an owned String, then explain why a string literal has a different lifetime.

Read the code

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

The annotation relates the inputs to the output.

Practice starter

fn dangle() -> &'static str {
    let text = String::from("local");
    text.as_str() // deliberate compile error
}
fn main() {}

Practise this code in Playground ↗

Try it

Correct the claim that a lifetime extends how long data lives.

What success looks like: You describe a constraint the compiler checks, not runtime behaviour.

Check your understanding

Why can no lifetime annotation make returning this reference valid?

Reveal the answer
  1. An explicit static lifetime extends the local allocation’s storage.
  2. The returned reference would outlive the local String allocation.
  3. Adding the same lifetime name to both bindings transfers ownership.

Annotations describe relationships; they do not keep a local allocation alive. Return String or borrow storage supplied by the caller.

Go deeper

Common misconception: Adding the same lifetime name to both bindings transfers ownership.

Lifetime names transfer nothing. text still owns the data and is dropped when the function returns, so no name on the signature can make the reference valid.

  1. Identify the contract: Lifetimes are about relationships, and saying so is the answer.
  2. Trace the concrete example: The annotation relates the inputs to the output.
  3. Test the changed constraint: Rewrite the function to return an owned String, then explain why a string literal has a different lifetime.

Transfer challenge: Rewrite the function to return an owned String, then explain why a string literal has a different lifetime.

Connections

  • Why Borrowing Works: Aliasing and mutation rules
  • Option vs Result: Absence, failure, and propagation
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 Reasoning (courseId "interview", lessonId "q-lifetimes")
Link: https://crabrix.com/learn/interview/q-lifetimes/
Goal: A lifetime constrains borrows; it never keeps anything alive.
Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=interview/q-lifetimes) 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.