lifetime

Rust Lifetime subtyping

A reference that is valid for a larger region can satisfy a request for a smaller one. This is a type relationship, not a change to the original owner. For example, a permanent shop name can be used alongside a customer name borrowed only during a visit. Expected output (not executed): Shop Why: The outlives constraint permits the shared reference conversion. Common mistake: Trying to perform the conversion in reverse, which would promise validity that the input cannot provide.

Formal Syntax & Structure

rust syntaxrust
'long: 'short

Working Example

Example Snippetrust
fn shorten<'long: 'short, 'short>(text: &'long str, _: &'short str) -> &'short str {    text}fn main() {    let customer = String::from("Mina");    println!("{}", shorten("Shop", &customer));}
Rust Reference