Rust Lifetime

Noun · Development

Definitions

  1. A compile-time annotation (written as 'a) that tells the Rust compiler how long a reference is valid, enabling it to prove that references never outlive the data they point to. Lifetimes are usually inferred by the compiler through elision rules, but must be explicit when the relationship between input and output references is ambiguous.

    In plain English: A label in Rust that tracks how long a piece of borrowed data is valid, so the compiler can prevent you from using data that has already been freed.

    Example: "The function signature fn longest<'a>(x: &'a str, y: &'a str) -> &'a str tells the compiler the returned reference lives as long as the shorter of the two inputs."

Related Terms