operator

Rust & and &mut: borrow a value

A shared borrow lets code read a value without taking ownership. A mutable borrow provides exclusive access for changes while that borrow is active, subject to Rust’s borrowing rules. Expected output (not executed): 5 7 Why: The shared borrow is no longer used when the mutable borrow begins. The change through the mutable reference updates the original. Common mistake: Do not keep using a shared reference while an overlapping mutable borrow is active. Advanced connection: Borrow operators cannot be overloaded.

Formal Syntax & Structure

rust syntaxrust
&place; &mut place

Working Example

Example Snippetrust
fn main() {    let mut quantity = 5;    let borrowed = &quantity;    println!("{borrowed}");    let editable = &mut quantity;    *editable += 2;    println!("{quantity}");}
Rust Reference