How to Read Lifetime Annotations in Rust

Read Rust lifetime annotations by matching the 'a label on input references to the output to ensure data validity.

Read lifetime annotations by identifying the 'a label on references to see which input reference must outlive the output reference. The compiler uses these labels to ensure that a returned reference does not point to data that will be dropped before it is used.

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

In this example, 'a binds the input references x and y to the return value, guaranteeing the returned string slice is valid as long as either input is valid.