Looking for a structured path? Browse all Rust lessons.
- Maintained by
- Learning Platform content team
- Reviewed by
- Learning Platform source and executable-example contract
Fix Rust E0499: overlapping mutable borrows
Rust permits many shared references or one mutable reference to a value at a time. E0499 appears when two mutable borrows overlap.
Reproduce it
fn main() {
let mut scores = vec![10, 20, 30];
let first = &mut scores[0];
let second = &mut scores[1];
*first += 1;
*second += 1;
}
Indexing does not prove to the borrow checker that 0 and 1 are disjoint, so second conflicts with the live first borrow.
Fix disjoint collection access with split_at_mut
fn main() {
let mut scores = vec![10, 20, 30];
let (left, right) = scores.split_at_mut(1);
left[0] += 1;
right[0] += 1;
println!("{scores:?}");
}
Expected output:
[11, 21, 30]
split_at_mut is a safe API whose type contract proves that the two slices cannot overlap.
Fix sequential updates by shortening the borrow
fn main() {
let mut total = 0;
{
let current = &mut total;
*current += 1;
}
let next = &mut total;
*next += 2;
println!("{total}");
}
Modern Rust ends a borrow after its final use, but an explicit scope is useful when a later operation still conflicts. For structs, borrowing distinct fields directly usually works; hiding the whole struct behind a method can make the borrow broader than intended.
Failure mode: reaching for unsafe code
Raw pointers can bypass E0499, but they also bypass the guarantee the error protects. Use the collection's safe split/entry APIs or restructure the update before considering unsafe.
Try the examples in the playground, then work through borrowing and collections.