How to read lifetime annotations

Lifetime annotations in Rust define how long references must remain valid to prevent memory safety errors.

Read lifetime annotations by identifying the 'a label on references to understand which data must outlive the function or struct. The compiler uses these labels to ensure references remain valid for the duration specified by the annotation.

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

In this example, 'a declares that the returned reference must live as long as the shortest of the two input references x and y.