borrowing
Rust Borrow a temporary in an extending let initializer
Certain let initializer forms extend the drop scope of a temporary. A direct borrow of a newly created String in this initializer keeps that temporary alive for the enclosing block. The rule is syntactic; arbitrary function calls that return borrowed references do not necessarily extend it. Expected output (not executed): Ready Why: The String temporary has its drop scope extended by this direct borrow initializer. Common mistake: Assuming a call such as identity(&String::from("Ready")) has the same extension behavior.
Formal Syntax & Structure
rust syntaxrust
let view = &String::from("text");Working Example
Example Snippetrust
fn main() { let view = &String::from("Ready"); println!("{view}");}