0Pricing
Learn Rust Coding · Lesson

Lifetime Annotations

Naming lifetimes.

Lifetime Annotations is a free Learn Rust Coding lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Naming a Lifetime

A lifetime annotation is a name starting with an apostrophe, like 'a. It does not change how long anything lives; it describes relationships between the lifetimes of references.

Where Annotations Go

You declare lifetime parameters in angle brackets after the function name, then use them on the reference types, just like generic type parameters.

Syntax: fn name<'a>(x: &'a T) -> &'a T.

The Classic longest Function

A function returning one of two references needs an annotation. 'a says the result lives as long as the shorter of the two inputs.

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

fn main() {
    let a = String::from("long string");
    let b = String::from("short");
    println!("{}", longest(&a, &b));
}

What 'a Means Here

The annotation tells the compiler: the returned reference is valid only while both inputs are valid. The compiler then checks every call site against this contract.

Why It Is Needed

Without the annotation, the compiler cannot know whether the return borrows from x or y. The lifetime name links them so the borrow checker can reason about the result.

Different Lifetimes

When references are unrelated, give them different lifetime names. Here only x is returned, so only its lifetime matters for the result.

fn first<'a, 'b>(x: &'a str, _y: &'b str) -> &'a str {
    x
}

fn main() {
    let a = String::from("keep me");
    let b = String::from("ignore");
    println!("{}", first(&a, &b));
}

Lifetimes Do Not Extend Life

Annotations never make data live longer. They only state constraints the compiler must verify. If a value dies too soon, the code will not compile regardless of annotations.

A Valid Call

As long as both inputs outlive the use of the result, the call is accepted. Here both strings live through the print, so all is well.

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

fn main() {
    let a = String::from("abcdef");
    let result;
    {
        let b = String::from("xy");
        result = longest(&a, &b);
        println!("chosen: {}", result);
    }
}

Lifetimes With Generics

Lifetime and type parameters can appear together. Lifetimes are listed first inside the angle brackets.

use std::fmt::Display;

fn announce<'a, T: Display>(text: &'a str, value: T) -> &'a str {
    println!("value is {}", value);
    text
}

fn main() {
    let msg = String::from("hello");
    println!("{}", announce(&msg, 42));
}

The 'static Lifetime

'static is a special lifetime meaning the reference can live for the entire program. String literals have it because they are baked into the binary.

fn motto() -> &'static str {
    "fearless concurrency"
}

fn main() {
    println!("{}", motto());
}

Reading Annotations

Read &'a str as a string reference valid for lifetime 'a. When two parameters share 'a, the compiler ties their lifetimes to whichever is shorter at each call.

Quick Check

Test your understanding of lifetime annotations.

Recap

You learned to name lifetimes:

  • Lifetimes use names like 'a, declared in angle brackets
  • Shared names express that references relate (e.g. result tied to inputs)
  • Annotations describe, never extend, lifetimes
  • 'static means valid for the whole program

Frequently asked questions

Is the “Lifetime Annotations” lesson free?

Yes — the full text of “Lifetime Annotations” is free to read here on the web, and the Learn Rust Coding course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.

What will I learn in “Lifetime Annotations”?

Naming lifetimes. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn Rust Coding?

No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Lifetime Annotations” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn Rust Coding lesson?

Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Why Lifetimes
  2. Lifetime Annotations
  3. Lifetimes in Structs
  4. Elision Rules
← Back to Learn Rust Coding