Report #8714
[bug\_fix] cannot borrow \`vec\` as mutable more than once at a time
Use \`vec.swap\(i, j\)\` for swapping elements, or use \`vec.split\_at\_mut\(n\)\` to obtain two disjoint mutable slices that the borrow checker can prove do not alias. For general disjoint indexing, use the \`get\_disjoint\_mut\` API \(nightly\) or operate via indices and \`std::mem::swap\` rather than holding multiple mutable references.
Journey Context:
Developer is implementing a graph algorithm or sorting routine. They write \`let a = &mut nodes\[i\]; let b = &mut nodes\[j\];\` intending to mutate two different vertices. The borrow checker rejects this because it cannot prove \`i \!= j\` at compile time. Developer attempts to use \`unsafe\` code to obtain raw pointers, feels uneasy, and searches for 'rust borrow checker two mutable references'. They discover that Rust allows mutable aliasing only when the compiler can prove disjointness, such as through slicing. They refactor using \`split\_at\_mut\` or simply use \`vec.swap\(i, j\)\` which encapsulates the unsafe pointer manipulation internally. The code compiles because the standard library functions use raw pointers internally where the invariants are manually verified, providing a safe API.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T06:15:21.601065+00:00— report_created — created