Rust Abstractions · 19 min · free lesson

Lifetimes

Lifetime annotations connect the borrows in a signature.

Core idea

A struct holding a reference is bounded by that reference's lifetime.

Diagram for Lifetimes: a Rust code example flows to the lesson rule and the key observation.
The struct is tied to the text it borrows.

Understand it

Elision handles the common shapes, so you write annotations only when the relationship is genuinely ambiguous — typically when several references go in and one comes out. A struct holding a reference needs one too, because the struct cannot outlive what it borrows.

A reference field makes a struct dependent on data owned elsewhere. Excerpt<'a> says its part cannot be used after the borrowed text becomes invalid. The annotation documents and checks a relationship; it does not allocate, copy, or lengthen the source string. If an Excerpt must outlive the source, the model should own a String instead of holding &str.

In the example, part(&self) returns a view of the existing borrowed text. The method receiver is borrowed, so the caller keeps the Excerpt; the result remains tied to the validity of both the struct and the underlying source. Try constructing an excerpt from a string created inside an inner block and moving the excerpt outside. The compiler rejects that use because the owner ends first. Move the use inside the block, or change the struct to own its data. Neither fix requires an invented longer lifetime label.

Lifetime elision saves annotations in signatures with an obvious input-output relationship. Explicit names become useful when a returned reference could come from several inputs, or when a type stores references. A name such as 'a means the linked borrows must satisfy the same validity relationship; it does not mean they were created at the same time or live for exactly the same duration. Draw the scope of each owner, then the interval where the reference is used. The intersection of those intervals is the practical boundary to reason about.

Read the code

struct Excerpt<'a> {
    part: &'a str,
}

impl<'a> Excerpt<'a> {
    fn part(&self) -> &str {
        self.part
    }
}

The struct is tied to the text it borrows.

Practice starter

struct Holder {
    value: &str,
}

Practise this code in Playground ↗

Try it

Store a borrowed slice in a struct and explain the annotation.

What success looks like: You can write the annotated Holder and explain why it cannot outlive the data it points into.

Check your understanding

Why is that struct definition rejected?

Reveal the answer
  1. A struct field can never hold a &str
  2. The struct must declare the lifetime it borrows for
  3. The value field has to be declared mut

Write struct Holder<'a> { value: &'a str } so the relationship is stated.

Go deeper

Common misconception: “'a keeps the borrowed text alive for as long as the struct needs it.”

It works the other way round: Excerpt<'a> cannot outlive the text it borrows, and the annotation extends nothing. If the struct must outlive its source, store a String.

  1. Find the borrow: Excerpt<'a> owns no text. Its part field borrows a &'a str from elsewhere, and part(&self) hands back that view.
  2. Predict before checking: Build text as a String in an inner block, store Excerpt { part: &text } in a variable declared outside it, and print it after the block. Predict whether that compiles.
  3. Compare with evidence: It fails with E0597: text does not live long enough. Printing inside the block compiles, and so does changing the field to an owned String.

Transfer challenge: Write struct Holder<'a> { value: &'a str } and add a first_word(&self) -> &str method. Then replace &'a str with String and note which annotations and call sites change.

Connections

  • Traits: Shared behavior and bounds
  • Closures: Fn, FnMut, FnOnce, and capture
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: Lifetimes (courseId "ownership", lessonId "lifetimes")
Link: https://crabrix.com/learn/ownership/lifetimes/
Goal: A struct holding a reference is bounded by that reference's lifetime.
Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=ownership/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.