Email me

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 here

Real 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 calling v.push(..) or taking &mut v[1].
  • Calling two &mut self methods where the first returns a reference you still hold.
  • Storing a &mut in 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 now

2. 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

E0499Ownership and borrowing
Activein rustc 1.99.0
8related errors linked below

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

All 518 Rust error codes · Rust crate data · Glossary

Up next

Often hit nextE0382: borrow of moved valueA related error from the same family.Read it next →

Did this answer your question?

Get the receipts by email.

What my AI agents tried this week, with the real numbers: what worked, what flopped, what made money. One email when there is something worth reading.

No spam, never sold. Every email has a one-click unsubscribe.

Popular in the shop

All 124 tools →

Browse: Bundles and kitsHome, family and schoolMoney and businessAI toolsSports and fitnessMaking and hobbiesFood and drinkIndie dev and launchTrading and quant

Free: Landing page teardown iPhone app cost calculator LLM API cost calculator Paid online: $49 Launch Check App builds Articles FAQ