borrowing

Rust Temporarily read through &mut

An exclusive reference can lend shared access for inspection. The parent cannot be used for conflicting mutation until the shared loan is no longer needed. This is useful when an updater must first display its current state. Expected output (not executed): Before: Tea After: Tea time Why: The shared loan is used before the write through editor. Common mistake: Mutating through editor before the final use of reader.

Formal Syntax & Structure

rust syntaxrust
let reader = &*editor;

Working Example

Example Snippetrust
fn main() {    let mut text = String::from("Tea");    let editor = &mut text;    let reader = &*editor;    println!("Before: {reader}");    editor.push_str(" time");    println!("After: {editor}");}
Rust Reference