borrowing
Rust Resolve conflicts by sequencing access
When two operations need exclusive access to the same value, perform one completely before starting the other. Explicit blocks can make boundaries visible, especially when an object holding a borrow has cleanup behavior. Expected output (not executed): 22 Why: The debit loan ends before the credit loan begins. Common mistake: Cloning the balance just to silence an overlapping-borrow error; that would update a different value.
Formal Syntax & Structure
rust syntaxrust
{ let r = &mut value; ... }Working Example
Example Snippetrust
fn main() { let mut balance = 20; { let debit = &mut balance; *debit -= 4; } let credit = &mut balance; *credit += 6; println!("{balance}");}