What is two-phase borrowing

Two-phase borrowing is not a standard Rust term; it likely refers to the borrow checker's rules preventing simultaneous mutable and immutable access to data.

Two-phase borrowing is not a standard Rust concept; you are likely thinking of the borrow checker's two-phase analysis or the distinction between immutable and mutable borrows. Rust enforces that you cannot have a mutable borrow while any immutable borrows exist, and vice versa, to ensure memory safety without a garbage collector. This rule is checked at compile time to prevent data races and use-after-free errors.

let mut data = vec![1, 2, 3];
let immutable_ref = &data; // Phase 1: Immutable borrow
// let mutable_ref = &mut data; // ERROR: Cannot borrow `data` as mutable because it is also borrowed as immutable

If you need to modify data after reading it, you must ensure the immutable borrow ends before starting the mutable one.