Rust

E0500 Closure Unique Access Fix

A borrow of the value is still alive, and the closure needs the value exclusively - the borrow is a shared reborrow, an element reference, a reading closure, an iterator, or a second reference to the same mutable reference. This guide separates five shapes that produce E0500 and matches each one to the same repair in five positions: finish with the borrow before the closure exists, so the closure's unique access no longer overlaps it.

Written November 24, 2026 · Tested with Rust 1.99.1 · 22 min read

By the end of this guide
  • The rule underneath: unique access cannot overlap any other borrow
  • How to read the diagnostic
  • The first failure: a shared reborrow of a mutable-reference parameter
  • The second failure: an element reference held across a pushing closure

Written November 24, 2026

Tested with Rust 1.99.1

What this guide is

Error E0500 is a closure that requires unique access to a value that is already borrowed. The error codes index states it in one line: a borrowed variable was used by a closure. Its example takes a &mut i32 parameter, borrows it again as a shared reference, and then builds a closure that assigns through the original mutable reference - and draws this error, because the shared borrow is still alive when the closure is constructed. Its fixes are to create the closure after the borrow has ended, or, where the type implements Clone, to clone the value between closures.

The five forms in this guide each reproduce E0500 with a small, complete program, and the borrow that blocks the closure has a different shape in each one. A shared reborrow of a &mut parameter, used again after the closure. A reference to one element of a vector, held across a closure that pushes. A first, reading closure that stays alive while a second closure tries to write. An iterator borrowed from the vector, counted only after a pushing closure. And the same shape built entirely inside main, from a locally created &mut. In each family the failing program is small, the full compiler output is shown, and the fix ends the blocking borrow before the closure is created - usually by moving its last use above the closure.

Four neighboring shapes are not E0500 and are not in the families. The same program written against the variable directly, with no reference in between, is E0502. Two mutable borrows of one variable at once is E0499. Calling a method that needs &mut through a shared reference is E0596 (assigning through a shared reference, *r = 2, is E0594 instead), and a closure returned from the function that owns its capture is E0373. The traps section shows each one with its real code. One further program in the traps ends the borrow before the closure is created, and compiles - it is the boundary of this error, because by the time the closure exists there is no other borrow left to conflict with it.

Build every program in this guide with rustc --edition 2021. In the compiler transcripts below, the file names are the bare names shown on the command line.

The rule underneath: unique access cannot overlap any other borrow

The borrowing rules, as The Rust Book's borrowing chapter states them, allow either any number of immutable references or exactly one mutable reference to a value at a time - never a mutable reference while an immutable one is alive, because readers do not expect the value to change under them. A reference's scope runs from where it is introduced through its last use, so a borrow ends at its last use, not at the end of its block.

A closure that mutates a captured value needs unique access to that value, and it needs that access from the moment the closure is constructed, not from the moment it is called. That is the detail this error turns on: the capture happens where the closure is written. If any other borrow of the same value is still alive at that line - alive meaning it has a use still to come - the construction itself is the conflict, and calling the closure later, or never, changes nothing.

Every family in this guide reaches the value through a mutable reference, and that indirection is what makes the code E0500 rather than its neighbor E0502. Where a closure mutates a variable it captures directly and the borrow still alive is a shared one, the compiler reports a mutable borrow conflicting with an immutable one and names E0502; if the borrow still alive is a mutable one, the same shape is E0499 instead. Where the closure's unique access is to a place behind a reference - *p, *v - and that place is already borrowed, the report is E0500, "closure requires unique access". The repair is the same in both cases; the code differs because of how the closure reaches the value.

Ending the borrow is a matter of last uses. Moving the blocking borrow's final use above the closure line ends it there, and the closure below it is legal - that is the index's own fix, and it is the fix most families here use. Where the value the borrow reads is still needed after the closure runs, copy it out first: read the element into a variable of its own, or take the iterator's count, before the closure exists. What does not work is wrapping lines in braces without moving the last use - the borrow lives exactly as long as its uses, no longer and no shorter.

How to read the diagnostic

The header names the place the closure needs: "closure requires unique access to *p but it is already borrowed". Read the spans in the order they happened. The first, "borrow occurs here", marks the borrow that is in the way - a reborrow expression, an element reference, a reading closure, an iterator. The second, "closure construction occurs here", underlines the closure's parameter list on the line the closure is defined. The third, "second borrow occurs due to use of *p in closure", sits on the mutation inside the body - the write that makes the closure require unique access rather than a shared one. The last span, set off by ... because intervening lines are elided, is "first borrow later used here": the use that keeps the first borrow alive across the closure.

That last span is the repair instruction. Whatever it underlines is the use to move. If it can move above the closure - a print, a call, a count - the fix is a reordering and nothing else changes. If it cannot, because its result depends on the state after the closure runs, the borrow and the closure genuinely need the value in overlapping windows, and the program needs restructuring instead: copy the borrowed data out before the closure, or split the work so the closure runs in a phase of its own. The two-closures family is the clearest read of all four spans: the first closure is the "borrow occurs here", and its later call is the use that keeps it alive.

The first failure: a shared reborrow of a mutable-reference parameter

The function receives &mut i32, immediately reborrows it as a shared reference, and then builds a closure that assigns through the original reference. The shared reborrow is printed after the closure runs, so it is alive at the closure's line:

Rust
fn bump(p: &mut i32) {
    let seen = &p;
    let mut set = || {
        *p = 3;
    };
    set();
    println!("{}", seen);
}

fn main() {
    let mut v = 1;
    bump(&mut v);
}
Terminal
$ rustc --edition 2021 fail_param.rs -o fail_param
error[E0500]: closure requires unique access to `*p` but it is already borrowed
 --> fail_param.rs:3:19
  |
2 |     let seen = &p;
  |                -- borrow occurs here
3 |     let mut set = || {
  |                   ^^ closure construction occurs here
4 |         *p = 3;
  |         -- second borrow occurs due to use of `*p` in closure
...
7 |     println!("{}", seen);
  |                    ---- first borrow later used here

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0500`.

This is the index's example with the names shortened, and the spans follow its shape exactly: &p borrows the place behind p, and the closure's assignment to *p needs that same place uniquely. The fix is the index's fix - use the reborrow before the closure exists, so its last use is above the closure line:

Rust
fn bump(p: &mut i32) {
    let seen = &p;
    println!("{}", seen);
    let mut set = || {
        *p = 3;
    };
    set();
    println!("{}", p);
}

fn main() {
    let mut v = 1;
    bump(&mut v);
}
Terminal
$ rustc --edition 2021 fix_param.rs -o fix_param
$ ./fix_param
1
3

The output is 1, the value the reborrow read before it ended, then 3, the value the closure assigned, printed through p itself after the closure ran. Nothing about the closure changed - only the position of the borrow's last use. That is the pattern to try first on any E0500: find the "first borrow later used here" span and ask whether that use can happen earlier.

The second failure: an element reference held across a pushing closure

The borrow here is a reference to one element of the vector. The closure pushes through the &mut Vec - an operation that can reallocate the vector's storage, which is exactly why a live element reference and a push cannot coexist:

Rust
fn grow(v: &mut Vec<i32>) {
    let first = &v[0];
    let mut add = || {
        v.push(4);
    };
    add();
    println!("{}", first);
}

fn main() {
    let mut v = vec![1, 2, 3];
    grow(&mut v);
    println!("{:?}", v);
}
Terminal
$ rustc --edition 2021 fail_vec.rs -o fail_vec
error[E0500]: closure requires unique access to `*v` but it is already borrowed
 --> fail_vec.rs:3:19
  |
2 |     let first = &v[0];
  |                  - borrow occurs here
3 |     let mut add = || {
  |                   ^^ closure construction occurs here
4 |         v.push(4);
  |         - second borrow occurs due to use of `*v` in closure
...
7 |     println!("{}", first);
  |                    ----- first borrow later used here

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0500`.

The span on &v[0] is a single dash - the borrow is the indexing expression - and the blocking use is the print after the closure call. Printing the element before the closure is built ends the borrow at that point:

Rust
fn grow(v: &mut Vec<i32>) {
    let first = &v[0];
    println!("{}", first);
    let mut add = || {
        v.push(4);
    };
    add();
}

fn main() {
    let mut v = vec![1, 2, 3];
    grow(&mut v);
    println!("{:?}", v);
}
Terminal
$ rustc --edition 2021 fix_vec.rs -o fix_vec
$ ./fix_vec
1
[1, 2, 3, 4]

The output is 1, the first element read while the reference was alive, then the vector after the push, [1, 2, 3, 4]. Where the element's value is needed after the push rather than before it, copy instead of reordering: let first = v[0]; takes the value itself - i32 is Copy - and no borrow survives to conflict with the closure at all. For element types that are not Copy, clone() is the same move at a stated cost, and cloning between closures is the index's second listed fix.

The third failure: a reading closure kept alive past a writing closure

Here the blocking borrow is itself a closure. The first closure reads through the reference, which borrows *p immutably for as long as that closure lives - and it lives until its call, which the program places after the writing closure:

Rust
fn run(p: &mut i32) {
    let get = || *p;
    let mut set = || {
        *p = 9;
    };
    set();
    println!("{}", get());
}

fn main() {
    let mut v = 1;
    run(&mut v);
}
Terminal
$ rustc --edition 2021 fail_two_closures.rs -o fail_two_closures
error[E0500]: closure requires unique access to `*p` but it is already borrowed
 --> fail_two_closures.rs:3:19
  |
2 |     let get = || *p;
  |               -- -- first borrow occurs due to use of `*p` in closure
  |               |
  |               borrow occurs here
3 |     let mut set = || {
  |                   ^^ closure construction occurs here
4 |         *p = 9;
  |         -- second borrow occurs due to use of `*p` in closure
...
7 |     println!("{}", get());
  |                    --- first borrow later used here

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0500`.

The diagnostic carries two markers on the get line: the closure itself is the borrow, and the *p inside it is labeled the first borrow. A closure is not a passive value - while it exists uncalled, its captures are held. Calling get before set is defined spends the read and ends that borrow:

Rust
fn run(p: &mut i32) {
    let get = || *p;
    println!("{}", get());
    let mut set = || {
        *p = 9;
    };
    set();
    println!("{}", p);
}

fn main() {
    let mut v = 1;
    run(&mut v);
}
Terminal
$ rustc --edition 2021 fix_two_closures.rs -o fix_two_closures
$ ./fix_two_closures
1
9

The output is 1, the value get read, then 9, the value set assigned, printed through p after both closures are done. If the read is needed both before and after the write, a closure is the wrong holder for it: take the value into a plain variable before the writing closure exists, or make the reading closure a move-free function of an explicit argument, so no capture spans the write.

The fourth failure: an iterator still alive when the closure is built

An iterator over the vector is a borrow of the whole vector for as long as the iterator can still be used. This program counts the iterator only after the pushing closure, so at the closure's line the iterator is still a live borrow of *v:

Rust
fn run(v: &mut Vec<i32>) {
    let total: i32 = v.iter().sum();
    let it = v.iter();
    let mut add = || {
        v.push(total);
    };
    add();
    println!("{}", it.count());
}

fn main() {
    let mut v = vec![1, 2];
    run(&mut v);
    println!("{:?}", v);
}
Terminal
$ rustc --edition 2021 fail_iter.rs -o fail_iter
error[E0500]: closure requires unique access to `*v` but it is already borrowed
 --> fail_iter.rs:4:19
  |
3 |     let it = v.iter();
  |              - borrow occurs here
4 |     let mut add = || {
  |                   ^^ closure construction occurs here
5 |         v.push(total);
  |         - second borrow occurs due to use of `*v` in closure
...
8 |     println!("{}", it.count());
  |                    -- first borrow later used here

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0500`.

Note what is not the problem: the first iterator, the one summed into total, is consumed by sum() on its own line and never conflicts - a borrow that is finished before the closure is invisible to this error. Only the second iterator, kept in a binding for later, blocks. Taking its count before the closure exists finishes it the same way:

Rust
fn run(v: &mut Vec<i32>) {
    let total: i32 = v.iter().sum();
    let it = v.iter();
    println!("{}", it.count());
    let mut add = || {
        v.push(total);
    };
    add();
}

fn main() {
    let mut v = vec![1, 2];
    run(&mut v);
    println!("{:?}", v);
}
Terminal
$ rustc --edition 2021 fix_iter.rs -o fix_iter
$ ./fix_iter
2
[1, 2, 3]

The output is 2, the count of the two elements the iterator covered, then the vector after the closure pushed their sum, [1, 2, 3]. count() consumes the iterator, which is what makes the reordering clean here. Where the iteration genuinely has to resume after a mutation, the iterator cannot span the closure at all: collect what it would produce into a new vector first, and iterate over that instead.

The fifth failure: a locally created mutable reference

No parameter is involved in this family. main creates the &mut itself, takes a second shared reference to that reference, and then builds the mutating closure - the same conflict, assembled entirely from local bindings:

Rust
fn main() {
    let mut x = 1;
    let p = &mut x;
    let r = &p;
    let mut c = || {
        *p = 2;
    };
    c();
    println!("{}", r);
}
Terminal
$ rustc --edition 2021 fail_local.rs -o fail_local
error[E0500]: closure requires unique access to `*p` but it is already borrowed
 --> fail_local.rs:5:17
  |
4 |     let r = &p;
  |             -- borrow occurs here
5 |     let mut c = || {
  |                 ^^ closure construction occurs here
6 |         *p = 2;
  |         -- second borrow occurs due to use of `*p` in closure
...
9 |     println!("{}", r);
  |                    - first borrow later used here

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0500`.

The header names *p, not x: the closure's unique access is to the place behind the local mutable reference, and &p is the borrow standing in its way. Ending that borrow before the closure - printing r first - is the whole fix, and after the closure runs the final value is read from x, the owner, because p's own borrow ended when the closure captured it:

Rust
fn main() {
    let mut x = 1;
    let p = &mut x;
    let r = &p;
    println!("{}", r);
    let mut c = || {
        *p = 2;
    };
    c();
    println!("{}", x);
}
Terminal
$ rustc --edition 2021 fix_local.rs -o fix_local
$ ./fix_local
1
2

The output is 1, read through the shared reference before it ended, then 2, the value the closure wrote, read from x. This family is the one to recognize in real code, where the &mut usually arrives from an earlier call rather than a parameter list: the shape of the conflict does not depend on where the reference came from, only on the second borrow outliving the closure's construction.

The fixes in reach order

  • The "first borrow later used here" span underlines a print, a call, or a count that can happen earlier: move that use above the closure. The borrow ends at its last use, and the closure below it is legal.
  • The blocking borrow reads a value that is still needed after the closure runs: copy the value out before the closure exists - read the element, take the count, or clone() where the type implements Clone, as the index's second fix describes.
  • The blocking borrow is a reading closure or an iterator: consume it before the writing closure is defined - call it, count it, collect it - or restructure so it does not span the write.
  • The two uses genuinely overlap, and neither can move: the design, not the ordering, is the problem - split the phases, or pass the value to the closure as an argument instead of capturing the reference.
  • The report is E0502, E0499, E0596, or E0373 instead: a different rule is doing the blocking - see the traps.

Traps

  • The same intent written against the variable directly is E0502, not E0500. This program differs from the fifth family in one respect: the closure captures x itself instead of writing through a &mut binding, so the compiler frames the conflict as a mutable borrow against an immutable one. The error codes index gives the cause: a variable already borrowed with one mutability was borrowed again with a different mutability, and its fix is to have no other references to the variable before accessing it with a different mutability:
Rust
fn main() {
    let mut x = 1;
    let r = &x;
    let mut c = || {
        x = 2;
    };
    c();
    println!("{}", r);
}
Terminal
$ rustc --edition 2021 probe_direct.rs -o probe_direct
error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immutable
 --> probe_direct.rs:4:17
  |
3 |     let r = &x;
  |             -- immutable borrow occurs here
4 |     let mut c = || {
  |                 ^^ mutable borrow occurs here
5 |         x = 2;
  |         - second borrow occurs due to use of `x` in closure
...
8 |     println!("{}", r);
  |                    - immutable borrow later used here

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0502`.
Trap
Read the header to tell the two apart: E0500 names a place behind a reference (*p) and says the closure "requires unique access"; E0502 names the variable itself and says it "cannot borrow as mutable". The repair - end the other borrow first - is identical. - Two mutable borrows of one variable at the same time is E0499; in this program no closure is involved at all (a mutating closure can itself be the second mutable borrow, and that shape is E0499 too). The error codes index gives the rule behind it: a variable was borrowed as mutable more than once, and in Rust there can be many immutable references or one mutable reference, never two mutable ones whose lifetimes overlap:
Rust
fn main() {
    let mut x = 1;
    let a = &mut x;
    let b = &mut x;
    println!("{} {}", a, b);
}
Terminal
$ rustc --edition 2021 probe_two_mut.rs -o probe_two_mut
error[E0499]: cannot borrow `x` as mutable more than once at a time
 --> probe_two_mut.rs:4:13
  |
3 |     let a = &mut x;
  |             ------ first mutable borrow occurs here
4 |     let b = &mut x;
  |             ^^^^^^ second mutable borrow occurs here
5 |     println!("{} {}", a, b);
  |                       - first borrow later used here

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0499`.

Here the second borrow is a plain &mut expression underlined in full, not a closure's parameter list. Sequencing the two borrows so the first is last used before the second is created is the fix - the same last-use logic as E0500, without a capture anywhere. - Trying to mutate through a shared reference by calling a method that needs &mut is E0596. Nothing is captured and no second borrow exists: the function simply receives &String and calls a method that needs &mut String. The error codes index gives the cause: a non-mutable variable was borrowed mutably - here, the value behind a & reference - and the compiler's help makes the same point by rewriting the parameter as &mut:

Rust
fn change(s: &String) {
    s.push_str(", world");
}

fn main() {
    let s = String::from("hello");
    change(&s);
    println!("{}", s);
}
Terminal
$ rustc --edition 2021 probe_behind_ref.rs -o probe_behind_ref
error[E0596]: cannot borrow `*s` as mutable, as it is behind a `&` reference
 --> probe_behind_ref.rs:2:5
  |
2 |     s.push_str(", world");
  |     ^ `s` is a `&` reference, so it cannot be borrowed as mutable
  |
help: consider changing this to be a mutable reference
  |
1 | fn change(s: &mut String) {
  |               +++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0596`.

Where E0500 says a borrow exists that must end first, E0596 says the borrow in hand is the wrong kind. Reordering uses fixes only the first; this one needs the signature - and the call site - changed to &mut. - A closure that outlives the value it captures is E0373. The closure here is returned from its function, so it would run after y is gone; the error codes index gives the cause: a captured variable in a closure may not live long enough, because by default a closure captures the data it closes over by reference, and its usual solution is a move closure, which takes ownership of the capture:

Rust
fn make() -> impl Fn() -> i32 {
    let y = 2;
    || y + 1
}

fn main() {
    let c = make();
    println!("{}", c());
}
Terminal
$ rustc --edition 2021 probe_outlive.rs -o probe_outlive
error[E0373]: closure may outlive the current function, but it borrows `y`, which is owned by the current function
 --> probe_outlive.rs:3:5
  |
3 |     || y + 1
  |     ^^ - `y` is borrowed here
  |     |
  |     may outlive borrowed value `y`
  |
note: closure is returned here
 --> probe_outlive.rs:3:5
  |
3 |     || y + 1
  |     ^^^^^^^^
help: to force the closure to take ownership of `y` (and any other referenced variables), use the `move` keyword
  |
3 |     move || y + 1
  |     ++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0373`.

The conflict is about how long the capture lives, not about what else borrows it. Adding move, as the help directs, is the repair; no reordering of borrows inside make changes the fact that the closure leaves the function. - A borrow whose last use comes before the closure compiles. This program is the first family with one line moved: the reborrow is printed above the closure instead of below it, so by the time the closure is constructed the borrow is already over:

Rust
fn bump(p: &mut i32) {
    let seen = &p;
    println!("{}", seen);
    let mut set = || {
        *p = 3;
    };
    set();
}

fn main() {
    let mut v = 1;
    bump(&mut v);
    println!("{}", v);
}
Terminal
$ rustc --edition 2021 boundary_end.rs -o boundary_end
$ ./boundary_end
1
3

The output is 1, the reborrow's read, then 3, the value after the closure's assignment, printed by main from the owner. This is the boundary of E0500: the same borrow, the same closure, the same write - the only difference from the first family is which side of the closure line the borrow's last use falls on.

Checklist

  • The header read for the place named: "closure requires unique access to *p" means the closure writes through a reference; a variable named directly points at E0502 instead.
  • The spans followed in order: the borrow that occurs first, the closure construction, the write inside the closure, and the first borrow's later use.
  • The later use moved above the closure where it can move - and the borrow treated as ending at that use, not at the end of its block.
  • A value needed after the closure copied or cloned out before the closure exists, instead of keeping its borrow alive.
  • A reading closure or iterator spanning the write consumed first - called, counted, or collected - or restructured so it does not span it.
  • A report of E0499, E0596, or E0373 repaired by its own rule - sequence the mutable borrows, take &mut in the signature, or add move - not by reordering around a closure.
  • The fixed program run, and its output checked against what the failing program was trying to compute.

Sources

  • Rust, Error codes index: E0500 - a borrowed variable was used by a closure; in its example a &mut i32 parameter is borrowed again as a shared reference while a closure assigns through the original reference, and the fixes are to create the closure after the borrow has ended or, where the type implements Clone, to clone the value between closures.
  • The Rust Book, References and Borrowing - at any given time there can be many immutable references or one mutable reference to a value, a mutable reference cannot coexist with an immutable one because readers do not expect the value to change, and a reference's scope starts where it is introduced and continues through the last time it is used, so a borrow ends before a later conflicting borrow when its last use comes first.
  • Rust, Error codes index: E0502 - a variable already borrowed with one mutability was borrowed again with a different mutability; the fix is to ensure there are no other references to the variable before accessing it with a different mutability.
  • Rust, Error codes index: E0499 - a variable was borrowed as mutable more than once; there can be many immutable references or one mutable reference.
  • Rust, Error codes index: E0596 - a non-mutable variable was borrowed mutably, as when mutating the value behind a & reference; the fix is to make the binding mutable or take a mutable reference.
  • Rust, Error codes index: E0373 - a captured variable in a closure may not live long enough; closures capture closed-over data by reference by default, a returned closure would use its captures after the owning function has returned, and the usual solution is a move closure that takes ownership of the data.

Keep Exploring