borrowing

Rust Explicit reborrowing of &mut

Reborrowing creates a new loan through an existing reference. While the derived exclusive loan is in use, the parent reference cannot be used for conflicting access. It becomes usable again when the derived loan ends. Expected output (not executed): 15 Why: The use of shorter finishes before original is used again. The original reference was not moved into shorter. Common mistake: Using let shorter = original with no expected reference type and assuming this necessarily reborrows.

Formal Syntax & Structure

rust syntaxrust
let shorter = &mut *original;

Working Example

Example Snippetrust
fn main() {    let mut points = 10;    let original = &mut points;    let shorter = &mut *original;    *shorter += 2;    *original += 3;    println!("{points}");}
Rust Reference