Namara

Code daily. Without assist.

2026-08-20

double_borrow.rs

fn main() {
    let mut v = vec![1, 2, 3];
    let a = &mut v;
    let b = &mut v;
    a.push(4);
    b.push(5);
}

This doesn't compile. Fix it while still pushing both 4 and 5 onto v.

Answer

a and b are two simultaneous mutable borrows of v — Rust forbids that. There's no need to hold either borrow open; just call the methods directly:

fn main() {
    let mut v = vec![1, 2, 3];
    v.push(4);
    v.push(5);
}