Email me

Rust errors / Ownership and borrowing

Rust compiler error · checked with rustc 1.99.0

Rust error E0503: cannot use `value` because it was mutably borrowed

What it meansA value was used after it was mutably borrowed.

What rustc prints

error[E0503]: cannot use `value` because it was mutably borrowed
 --> main.rs:5:16
  |
4 |     let borrow = &mut value;
  |                  ---------- `value` is borrowed here
5 |     let _sum = value + 1; // error: cannot use `value` because
  |                ^^^^^ use of borrowed `value`
6 |                           //        it was mutably borrowed
7 |     println!("{}", borrow);
  |                    ------ 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.

E0503Ownership and borrowing
Activein rustc 1.99.0
7related errors linked below

The compiler team's explanation

A value was used after it was mutably borrowed.

Erroneous code example:

fn main() {
    let mut value = 3;
    // Create a mutable borrow of `value`.
    let borrow = &mut value;
    let _sum = value + 1; // error: cannot use `value` because
                          //        it was mutably borrowed
    println!("{}", borrow);
}

In this example, value is mutably borrowed by borrow and cannot be used to calculate sum. This is not possible because this would violate Rust's mutability rules.

You can fix this error by finishing using the borrow before the next use of the value:

fn main() {
    let mut value = 3;
    let borrow = &mut value;
    println!("{}", borrow);
    // The block has ended and with it the borrow.
    // You can now use `value` again.
    let _sum = value + 1;
}

Or by cloning value before borrowing it:

fn main() {
    let mut value = 3;
    // We clone `value`, creating a copy.
    let value_cloned = value.clone();
    // The mutable borrow is a reference to `value` and
    // not to `value_cloned`...
    let borrow = &mut value;
    // ... which means we can still use `value_cloned`,
    let _sum = value_cloned + 1;
    // even though the borrow only ends here.
    println!("{}", borrow);
}

For more information on Rust's ownership system, take a look at the References & Borrowing section of the Book.

From rustc --explain E0503 (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