borrowing

Rust Two-phase borrowing in eligible calls

Some compiler-generated mutable borrows reserve access while arguments are evaluated, then activate exclusive access at the call. A familiar case is a mutable method receiver with a read-only argument computed from the same receiver. This is compiler behavior, not permission to create arbitrary overlapping explicit &mut borrows. Expected output (not executed): [10, 20, 30, 3] Why: The length is obtained during argument evaluation before the mutable receiver loan is activated. Common mistake: Generalizing this to every explicit &mut expression; compute the length in a separate statement if needed.

Formal Syntax & Structure

rust syntaxrust
collection.push(collection.len());

Working Example

Example Snippetrust
fn main() {    let mut queue = vec![10_usize, 20, 30];    queue.push(queue.len());    println!("{queue:?}");}
Rust Reference