liveBook · Manning

Lifetime annotation

In Rust, a lifetime annotation is syntax that names or relates the validity lifetimes of references in types and function signatures.

How 2 Manning books explain it

Refactoring to Rust

A lifetime annotation is Rust syntax that names or relates the validity periods of references in types and function signatures.

How it is used
Clarifies which input a returned reference borrows from, including a string slice read from an NGINX request.
How deep it goes
Works through inferred and explicit signature relationships and borrow-checker errors, then applies them to an unsafe FFI helper.
Examples
Uses a vector-element helper, a rejected vector mutation, and an NGINX request-body conversion.
What you can do after
Apply it via Rust function signatures to relate a returned reference to the input whose memory it views.

Read how Refactoring to Rust explains lifetime annotation →

Learn Rust in a Month of Lunches

A lifetime annotation is Rust syntax that names relationships among the lifetimes of references and values in a type or function signature.

How it is used
Uses annotations to express reference validity in function signatures and data types when inference needs help.
How deep it goes
Explains reference validity, function and struct syntax, compiler errors, and 'static, but defers broader lifetime forms.
Examples
Uses invalid function returns, compiler diagnostics, string literals, and a City struct borrowing from a vector.
What you can do after
Diagnose missing lifetime errors and choose between owned data, &'static str, or a named lifetime parameter.

Read how Learn Rust in a Month of Lunches explains lifetime annotation →

Related concepts

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.