borrowing

Rust Non-lexical lifetimes: shorten a loan

Borrow checking considers how references are used along control flow, not simply where their binding names go out of scope. In straightforward code, a shared loan can cease to constrain writes after its last use. This does not imply the owner is dropped at that point. Expected output (not executed): Draft Draft saved Why: There is no later use of preview, so the subsequent write does not conflict with its loan. Common mistake: Assuming the loan always lasts to the closing brace, or that all loans always end at a single textual last-use point regardless of control flow and destructors.

Formal Syntax & Structure

rust syntaxrust
let r = &value; /* last use */; /* later mutation */

Working Example

Example Snippetrust
fn main() {    let mut title = String::from("Draft");    let preview = &title;    println!("{preview}");    title.push_str(" saved");    println!("{title}");}
Rust Reference