Email me

Rust errors / Ownership and borrowing

Rust compiler error · checked with rustc 1.99.0

Rust error E0501: cannot borrow `*a` as mutable because previous closure requires unique access

What it meansA mutable variable is used but it is already captured by a closure.

What rustc prints

error[E0501]: cannot borrow `*a` as mutable because previous closure requires unique access
  --> main.rs:13:21
   |
10 |     let mut bar = || {
   |                   -- closure construction occurs here
11 |         inside_closure(a)
   |                        - first borrow occurs due to use of `*a` in closure
12 |     };
13 |     outside_closure(a); // error: cannot borrow `*a` as mutable because previous
   |                     ^ second borrow occurs here
14 |                         //        closure requires unique access.
15 |     bar();
   |     --- 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.

E0501Ownership and borrowing
Activein rustc 1.99.0
6related errors linked below

The compiler team's explanation

A mutable variable is used but it is already captured by a closure.

Erroneous code example:

fn inside_closure(x: &mut i32) {
    // Actions which require unique access
}

fn outside_closure(x: &mut i32) {
    // Actions which require unique access
}

fn foo(a: &mut i32) {
    let mut bar = || {
        inside_closure(a)
    };
    outside_closure(a); // error: cannot borrow `*a` as mutable because previous
                        //        closure requires unique access.
    bar();
}

This error indicates that a mutable variable is used while it is still captured by a closure. Because the closure has borrowed the variable, it is not available until the closure goes out of scope.

Note that a capture will either move or borrow a variable, but in this situation, the closure is borrowing the variable. Take a look at the chapter on Capturing in Rust By Example for more information.

To fix this error, you can finish using the closure before using the captured variable:

fn inside_closure(x: &mut i32) {}
fn outside_closure(x: &mut i32) {}

fn foo(a: &mut i32) {
    let mut bar = || {
        inside_closure(a)
    };
    bar();
    // borrow on `a` ends.
    outside_closure(a); // ok!
}

Or you can pass the variable as a parameter to the closure:

fn inside_closure(x: &mut i32) {}
fn outside_closure(x: &mut i32) {}

fn foo(a: &mut i32) {
    let mut bar = |s: &mut i32| {
        inside_closure(s)
    };
    outside_closure(a);
    bar(a);
}

It may be possible to define the closure later:

fn inside_closure(x: &mut i32) {}
fn outside_closure(x: &mut i32) {}

fn foo(a: &mut i32) {
    outside_closure(a);
    let mut bar = || {
        inside_closure(a)
    };
    bar();
}

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