When Should I Use 'static vs a Named Lifetime?

Use static for data living the entire program duration and named lifetimes to relate the validity of multiple references.

Use static when a reference must live for the entire duration of the program, such as for string literals or global data; use a named lifetime (e.g., 'a) when you need to relate the lifetimes of multiple references to ensure they don't outlive the data they point to. Named lifetimes are required for functions or structs that borrow data from arguments or fields, whereas static is a specific, fixed lifetime bound to the binary.

// Use 'static for string literals (data lives forever)
fn greet() -> &'static str {
    "Hello, world!"
}

// Use named lifetime 'a to tie the output reference to the input reference
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
    if x.len() > y.len() { x } else { y }
}