Lifetimes

Rust is a systems programming language focused on speed, memory safety, and fearless concurrency — and lifetimes are how it guarantees, at compile time, that a reference never outlives the data it points to.

Learn Lifetimes in our free Rust course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Rust course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

In this lesson you'll learn what lifetimes describe, annotate functions and structs that hold references, and understand elision and the special 'static lifetime.

What You'll Learn in This Lesson

1️⃣ Lifetimes in Functions

When a function returns a reference, the compiler must know which input that reference borrows from, so it can guarantee the result doesn't outlive its source. A lifetime parameter like 'a expresses this: tying the inputs and output to the same 'a says "the result lives as long as the shorter-lived input".

The 'a doesn't make anything live longer — it just describes the relationship so the borrow checker can prove the returned reference is always valid.

2️⃣ Lifetimes on Structs, Elision, and 'static

A struct that holds a reference must declare a lifetime, ensuring it can't outlive the borrowed data. In many functions, though, you write no lifetimes at all thanks to elision — rules that let the compiler infer them. The special 'static lifetime means "valid for the whole program", which is the lifetime of string literals.

Note first_sentence needed no explicit lifetime — elision handled the single-reference case. The Excerpt<'a> struct, however, holds a reference, so its lifetime had to be written.

Your turn. Fill in the blanks marked ___ , then run it.

Extend the classic example to three inputs, annotating the shared lifetime. Run it with cargo run .

📋 Quick Reference — Lifetimes

Practice quiz

What does a lifetime describe?

  • How fast a function runs
  • The size of a value
  • How long a reference is valid
  • How much memory is used

Answer: How long a reference is valid. A lifetime describes the scope during which a reference stays valid.

Do lifetime annotations change how long values actually live?

  • No, they only describe relationships
  • Yes, they extend life
  • Yes, they shorten life
  • Only for 'static

Answer: No, they only describe relationships. Annotations describe relationships so the borrow checker can verify safety.

How is a lifetime parameter written?

  • <T>
  • <life>

A lifetime parameter looks like 'a, declared in angle brackets.

When does the compiler most often require a lifetime annotation?

  • When printing a value
  • When a function returns a reference from multiple inputs
  • When using mut
  • When calling a macro

Answer: When a function returns a reference from multiple inputs. It needs to know which input a returned reference borrows from.

What does fn longest<'a>(x: &'a str, y: &'a str) -> &'a str express?

  • The output lives as long as the shorter-lived input
  • The output outlives both inputs
  • x is copied
  • The strings are mutable

Answer: The output lives as long as the shorter-lived input. Tying inputs and output to 'a means the result is valid while both inputs are.

A struct that holds a reference must:

  • Be marked mut
  • Use a HashMap
  • Declare a lifetime
  • Implement Display

Answer: Declare a lifetime. Storing a reference requires a lifetime so the struct can't outlive the data.

What is lifetime elision?

  • Deleting references
  • Rules letting the compiler infer lifetimes in common cases
  • A runtime check
  • A kind of macro

Answer: Rules letting the compiler infer lifetimes in common cases. Elision rules infer lifetimes so you often write none at all.

What does the 'static lifetime mean?

  • Valid for one function
  • Never valid
  • A mutable reference
  • Valid for the entire program

Answer: Valid for the entire program. 'static means the reference is valid for the whole program duration.

What type do string literals have?

  • String
  • &'static str
  • &mut str
  • char

Answer: &'static str. String literals are &'static str because their text lives in the binary.

Is reaching for 'static usually the right fix for a lifetime error?

  • Yes, always
  • Only with mut
  • No, it rarely fixes the real problem
  • Only for structs

Answer: No, it rarely fixes the real problem. 'static rarely helps; the real issue is usually a value not living long enough.