borrowing

Rust Reuse &mut in typed function calls

At a call with an expected mutable-reference parameter, Rust can implicitly reborrow an existing mutable reference. The callee receives a loan suitable for the call, instead of permanently taking the caller’s reference. Expected output (not executed): 2 Why: Each call gets a short mutable reborrow, so r is available for the next call. Common mistake: Assuming every by-value argument expression moves a mutable reference regardless of its expected type.

Formal Syntax & Structure

rust syntaxrust
fn update(value: &mut T); update(r);

Working Example

Example Snippetrust
fn increment(n: &mut i32) {    *n += 1;}fn main() {    let mut total = 0;    let r = &mut total;    increment(r);    increment(r);    println!("{r}");}
Rust Reference