borrowing

Rust Exclusive access: update through &mut

A mutable reference lends access to a place for reading and updating. While that loan is needed, conflicting access to the same place is restricted. The original owner remains responsible for the value. Expected output (not executed): 3 Stock: 3 Why: The write through editor changes stock; later access to stock is allowed after the final use of editor. Common mistake: Expecting to read stock while an overlapping exclusive borrow must still be used later.

Formal Syntax & Structure

rust syntaxrust
let editor = &mut owner;

Working Example

Example Snippetrust
fn main() {    let mut stock = 4;    let editor = &mut stock;    *editor -= 1;    println!("{editor}");    println!("Stock: {stock}");}
Rust Reference