Ownership in Practice · 14 min · free lesson

Lifetime Elision

The common lifetime relationships Rust infers.

Core idea

Rust omits obvious lifetime annotations using a small set of rules.

Diagram for Lifetime Elision: a Rust code example flows to the lesson rule and the key observation.
The example makes the contract visible in code.

Understand it

Rust omits obvious lifetime annotations using a small set of rules. One borrowed input can usually determine the returned borrow, keeping signatures readable without weakening the proof.

Elision means the compiler fills in common lifetime relationships you did not write. In fn first(text: &str) -> &str, the only borrowed input is text, so the returned slice must be tied to it. The function cannot create a reference that outlives the caller's text. Writing the equivalent named lifetime can be educational, but it does not make the result live longer or change runtime behavior.

The simple rules become less obvious with two borrowed inputs. A function fn choose(a: &str, b: &str) -> &str leaves the compiler unable to know which input the result borrows from. If the body can return either, name a shared relationship in the signature. If the result always comes from a, make that explicit. Methods have a useful convention: when &self is an input, an elided output borrow is tied to self unless you specify otherwise. These are rules of signature interpretation, not guesses based on the function body.

Practice by writing the explicit version of first and checking that it compiles exactly as before. Then add a second borrowed parameter used only as a search needle; if the return still comes from text, annotate that relationship accurately. Draw arrows from the output reference back to the input that owns its data. This skill makes API signatures easier to read and avoids over-annotating code where elision already communicates the contract clearly.

Read the code

fn first(text: &str) -> &str {
    text.split(' ').next().unwrap_or(text)
}

The example makes the contract visible in code.

Practise this code in Playground ↗

Try it

Write out the lifetime the compiler infers for first, and say why it is unambiguous.

What success looks like: You can show the explicit signature, and say when a second borrowed input would require a name.

Check your understanding

Why does first need no named lifetime?

Reveal the answer
  1. Every &str already has a static lifetime
  2. One input determines output
  3. unwrap supplies the missing lifetime

Correct: One input determines output.

Go deeper

Common misconception: “first needs no lifetime because every &str is 'static.”

text can borrow from any String; only literals are 'static. The name can stay implied because with one input the result must borrow from text.

  1. Expand the signature: Elision reads fn first(text: &str) -> &str as fn first<'a>(text: &'a str) -> &'a str: there is one input, so the output borrows from it.
  2. Predict before checking: Write the explicit form and predict whether it behaves differently. Then add other: &str as a second parameter and predict what the elided form does.
  3. Compare with evidence: The explicit form compiles and returns the same slice. With two borrowed inputs, the elided signature gets E0106 until you name which one the output borrows.

Transfer challenge: Add a needle: &str parameter and call text.split(needle) in first. Annotate it so only text shares 'a with the result, then show that the result can outlive the needle.

Connections

  • Interior Mutability: Move a checked borrow rule to runtime
  • Owned or Borrowed?: Choose a return type that matches the data flow
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 Elision (courseId "ownership", lessonId "lifetime-elision")
Link: https://crabrix.com/learn/ownership/lifetime-elision/
Goal: Rust omits obvious lifetime annotations using a small set of rules.
Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=ownership/lifetime-elision) 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.