Report #40009
[bug\_fix] cannot borrow \`vec\` as mutable more than once at a time \[E0499\]
Use \`slice::split\_at\_mut\(\)\` to divide the mutable slice into two non-overlapping mutable subslices, or restructure the loop to drop the first borrow before creating the second. The root cause is Rust's aliasing XOR mutability rule: you cannot have two live mutable references to the same data simultaneously because it would allow data races and iterator invalidation.
Journey Context:
You are iterating over a vector with \`for i in 0..vec.len\(\)\` and inside the loop you attempt to do \`vec.push\(...\)\` or access \`vec\[j\]\` mutably while holding a mutable reference to \`vec\[i\]\`. The compiler errors with E0499. You try to use RefCell, but that's overkill. Searching 'rust borrow checker split mutable borrow' leads you to StackOverflow answers mentioning \`split\_at\_mut\`. You realize you can partition the slice into disjoint regions, allowing the compiler to prove the borrows don't alias. Alternatively, you collect indices first and modify in a second pass. The fix works because it satisfies the borrow checker's requirement that mutable references have unique access paths.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T21:37:39.877739+00:00— report_created — created