lifetime
Rust Keep a temporary owner available
A reference returned through a method or function can borrow a temporary that is dropped at the end of the statement. Binding the owned value first makes its drop scope explicit. Temporary lifetime extension is a syntax-sensitive rule, not a property of all let bindings. Expected output (not executed): Box 7 Why: The owned String remains in scope until after the trimmed view is printed. Common mistake: Using let label = make_label().trim() and expecting the temporary String to survive to the later println.
Formal Syntax & Structure
rust syntaxrust
let owner = make_value(); let view = owner.as_str();Working Example
Example Snippetrust
fn make_label() -> String { String::from(" Box 7 ") }fn main() { let owner = make_label(); let label = owner.trim(); println!("{label}");}