Rust errors / Ownership and borrowing
Rust compiler error · checked with rustc 1.99.0
Rust error E0499: cannot borrow as mutable more than once at a time
What it meansTwo &mut references to the same thing are alive at the same time. Rust allows one mutable borrow at a time so no two places can change the data under each other.
What rustc prints
error[E0499]: cannot borrow `i` as mutable more than once at a time
--> main.rs:5:13
|
4 | let mut x = &mut i;
| ------ first mutable borrow occurs here
5 | let mut a = &mut i;
| ^^^^^^ second mutable borrow occurs here
6 | x;
| - first borrow later used hereReal output of rustc 1.99.0 compiling the official example below (2021 edition). Your names and line numbers will differ; the first line is the part to match.
Why it happens
- Holding
let a = &mut v[0];while also callingv.push(..)or taking&mut v[1]. - Calling two
&mut selfmethods where the first returns a reference you still hold. - Storing a
&mutin a struct and then borrowing the original again.
How to fix it
1. Shorten the first borrow
Finish using the first reference before taking the second; non-lexical lifetimes end a borrow at its last use.
let first = &mut v[0];
*first += 1; // last use of `first`
v.push(4); // fine now2. Split the borrow
For two elements of one slice, split_at_mut gives two independent mutable halves.
let (left, right) = v.split_at_mut(1);
std::mem::swap(&mut left[0], &mut right[0]);3. Borrow fields separately
Borrowing two different fields of a struct directly is allowed; going through a &mut self method is not.
let Game { players, board } = &mut game;
players.push(p);
board.place(p.id);Other wordings of this error
cannot borrow as mutable more than once at a time, first mutable borrow occurs here, second mutable borrow occurs here
The compiler team's explanation
A variable was borrowed as mutable more than once.
Erroneous code example:
let mut i = 0;
let mut x = &mut i;
let mut a = &mut i;
x;
// error: cannot borrow `i` as mutable more than once at a time
Please note that in Rust, you can either have many immutable references, or one mutable reference. For more details you may want to read the References & Borrowing section of the Book.
Example:
let mut i = 0;
let mut x = &mut i; // ok!
// or:
let mut i = 0;
let a = &i; // ok!
let b = &i; // still ok!
let c = &i; // still ok!
b;
a;From rustc --explain E0499 (1.99.0), part of the Rust project, MIT or Apache-2.0. Official page.
Related errors
- E0382borrow of moved value
- E0502cannot borrow as mutable because it is also borrowed as immutable
- E0106missing lifetime specifier
- E0597does not live long enough
- E0493destructor of `(DropType, DropType)` cannot be evaluated at compile-time
- E0496lifetime name `'a` shadows a lifetime name that is already in scope
- E0500closure requires unique access to `*jon_snow` but it is already borrowed
- E0501cannot borrow `*a` as mutable because previous closure requires unique access
Up next
Often hit nextE0382: borrow of moved valueA related error from the same family.Read it next →Did this answer your question?