lifetime

Rust Named lifetime parameters

Declare a lifetime parameter in angle brackets when you need to express a relationship explicitly. A descriptive name such as 'text can make a signature easier to understand. The caller supplies a compatible borrow; the name is not a variable holding a duration. Expected output (not executed): Box 9 Why: The trimmed view borrows the original string and is bounded by its input borrow. Common mistake: Using an undeclared name or assuming naming it makes the result owned.

Formal Syntax & Structure

rust syntaxrust
fn label<'a>(s: &'a str) -> &'a str

Working Example

Example Snippetrust
fn label<'text>(s: &'text str) -> &'text str {    s.trim()}fn main() {    let raw = String::from("  Box 9  ");    println!("{}", label(&raw));}
Rust Reference