lifetime

Rust Generic type outlives a lifetime

A type bound T: 'a constrains the lifetimes carried by T. A borrowed parameter &'a T already implies this bound where needed for well-formedness. An owned type without non-static borrows often satisfies even stronger bounds. Expected output (not executed): tea Why: The reference to view is valid while view and its borrowed string remain usable. Common mistake: Interpreting T: 'a as an instruction to keep the particular value alive for that duration.

Formal Syntax & Structure

rust syntaxrust
fn inspect<'a, T: 'a>(value: &'a T)

Working Example

Example Snippetrust
fn inspect<'a, T: std::fmt::Display + 'a>(value: &'a T) {    println!("{value}");}fn main() {    let name = String::from("tea");    let view = name.as_str();    inspect(&view);}
Rust Reference