liveBook · Manning

Lifetime parameter

As explained in Idiomatic Rust

A named parameter that expresses constraints on the validity duration of references in a type or function.

A lifetime parameter names a reference’s validity constraint so the compiler can track it through use. In `Dog<'a>`, the `name` reference must remain valid for at least as long as the `Dog`. Lifetime parameters resemble generic parameters in angle-bracket syntax, but they are not the same. String literals have the special `'static` lifetime, which lasts for the program’s duration. section 2.1.5

struct Dog<'a> {
    name: &'a str,
}

This struct shows a lifetime parameter attached to a borrowed name. section 2.1.5

How Idiomatic Rust covers it

How deep it goes
Defines the parameter and explains its struct-field constraint, with a brief note on the special `'static` lifetime.
Examples
Uses `Dog<'a>` with a borrowed `name` field and notes that string literals have `'static` lifetime.
What you can do after
Apply a named lifetime to a struct reference and understand the validity constraint it expresses.
How it connects
Connects lifetime parameters to references, borrowing, generic parameters, and the `'static` lifetime.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Idiomatic Rust. The section links point to the parts of the book it draws on.