Rust
E0524 Two Closures Require Unique Access Fix
One mutable reference can serve only one live closure that needs unique access through it. The moment a second such closure exists while the first may still be called, the compiler stops the program with E0524 - before either closure runs. This guide works the shapes that produce it and matches each one to its repair: run the closures one at a time with a fresh reborrow in each, share the value through Rc and RefCell, hand the reference to the closures as a parameter, merge them into one closure, or finish the first closure before the second is defined.
Written November 29, 2026 · Tested with Rust 1.99.1 · 30 min read
- The rule underneath: unique access cannot be held twice at once
- How to read the diagnostic
- The first failure: the captured reference passed to a helper by two closures
- The second failure: two closures mutating a local &mut binding
Written November 29, 2026
Tested with Rust 1.99.1
What this guide is
Error E0524 is a variable which requires unique access being used in more than one closure at the same time. The error codes index opens with exactly that sentence, and its example is the shape this guide starts from: a function takes x: &mut isize, two closures c1 and c2 each capture x to pass it to the same helper, and the program is rejected - "two closures require unique access to *x at the same time". The index gives the two families of repair this guide uses throughout: if the variable really must serve more than one closure at a time, share it through a reference-counted type such as Rc (or Arc when it runs concurrently), here paired with RefCell; if not, run the closures one at a time.
One condition decides whether this error is the one that appears, and it is worth stating before any of the failures: in every E0524 shape here, the variable both closures capture is itself a reference - a &mut parameter, or a local binding that holds a &mut. Two closures needing unique access through the same lent-on reference is its own conflict, with its own code. The same program shapes built over an owned variable do not produce E0524 at all: two closures capturing an owned x or v mutably are E0499, an ordinary second mutable borrow. That owned-variable case is worked in the traps, side by side with this guide's failures, because telling the two apart is the first step of the repair: the fixes below are about how the closures share - or stop sharing - one reference.
A second condition is just as sharp: both closures must need unique access. A second closure that only reads through the reference does not require unique access, so it does not produce E0524 - it produces E0501, the one-closure conflict, and it is worked in the traps. And two closures that only read compile outright. The forms in this guide each reproduce E0524 with a small, complete program: the index's own two-closure example, two closures mutating a local &mut binding directly, two closures pushing through a &mut Vec, two closures passing a &mut struct to a helper, a first closure that is called again after the second is defined, and three closures over one reference. Each failure is followed by its repair.
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 be held twice at once
A closure that mutates through a captured mutable reference needs unique access to the value behind that reference every time it is called. While such a closure may still be called, it holds that unique access - and unique access, by definition, cannot be held by a second closure at the same time. The borrow rules in References and Borrowing allow one mutable reference to a value, never two; two live closures that each need to act as that one mutable reference are two holders of a thing only one holder may have.
The word that does the work in the header is "at the same time", and it is about the closures being live together, not about them running together. Neither closure has to run before the error fires: the second closure's definition is already the conflict, as long as the first closure is used afterward. Even a second closure that is never called at all still triggers the error, as the traps show - its construction is the second hold, and the first closure's later call proves the overlap. What ends the overlap is the first closure's last use: once a closure will never be called again, its hold is over, and a new closure over the same reference may be defined freely. Every repair in this guide works on one side of that line. Run the closures one at a time, each taking a fresh reborrow - &mut *x - at its own definition, so no closure holds the original reference across the other's lifetime. Or finish the first closure completely before the second is defined. Or remove the shared capture: pass the reference in as a parameter at call time, merge the two bodies into one closure, or move the value behind Rc<RefCell<...>> so each closure borrows it briefly, at call time, instead of holding it between calls.
How to read the diagnostic
The header names the place both closures need - *x, *a - and states the conflict in one line: "two closures require unique access to *x at the same time". Below it, the spans lay out the two holds. The span on the first closure's ||, labeled "first closure is constructed here" and "first borrow occurs due to use of *x in closure", marks where the first hold began. The caret span on the second closure, labeled "second closure is constructed here" and "second borrow occurs due to use of *x in closure", marks the definition that conflicts - note that it points at a definition, not a call: the error is complete before either closure runs. And a span under a later call of the first closure, labeled "first borrow later used here", is the proof that the first hold was still live when the second closure was constructed - it is the line to check first, because it names which closure's remaining calls keep the conflict alive.
That reading also separates this error from its neighbors before any repair starts. If only one closure exists and the conflicting act is a plain use of the reference outside it, the header is E0501, "previous closure requires unique access". If the two closures capture an owned variable rather than a reference, the header is E0499, "cannot borrow as mutable more than once at a time". If there are no closures at all, two mutable borrows are likewise E0499. And if the problem is data escaping a closure's body rather than two closures sharing a capture, the header is E0521. All four are worked in the traps.
The first failure: the captured reference passed to a helper by two closures
This is the error index's own example shape. dragoooon takes x: &mut isize; c1 captures x to hand it to set, and c2 captures the same x for the same helper. The second definition is already the error:
fn set(x: &mut isize) { *x += 4; }
fn dragoooon(x: &mut isize) {
let mut c1 = || set(x);
let mut c2 = || set(x);
c2();
c1();
}
fn main() { let mut v = 1; dragoooon(&mut v); println!("value is {v}"); }
error[E0524]: two closures require unique access to `*x` at the same time
--> a1.rs:4:18
|
3 | let mut c1 = || set(x);
| -- - first borrow occurs due to use of `*x` in closure
| |
| first closure is constructed here
4 | let mut c2 = || set(x);
| ^^ - second borrow occurs due to use of `*x` in closure
| |
| second closure is constructed here
5 | c2();
6 | c1();
| -- first borrow later used here
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0524`.
The repair the index prescribes for the one-at-a-time case is to run the closures in sequence, each closure reborrowing the value - set(&mut *x) - instead of capturing x itself. A reborrow lives only for the closure that made it, so when c1 has run and its block ends, x is free for c2 to reborrow in turn:
fn set(x: &mut isize) { *x += 4; }
fn dragoooon(x: &mut isize) {
{
let mut c1 = || set(&mut *x);
c1();
}
let mut c2 = || set(&mut *x);
c2();
}
fn main() { let mut v = 1; dragoooon(&mut v); println!("value is {v}"); }
value is 9
The output is value is 9: the caller's variable starts at 1, each closure adds 4, and both increments land - the same total the failing program intended, reached by two holds that never overlap.
The second failure: two closures mutating a local &mut binding
The captured reference does not have to arrive as a parameter. Here a is a local &mut binding, and two closures each mutate through it directly - one adds 1, the other adds 10:
fn main() {
let mut v = 1;
let a = &mut v;
let mut c1 = || { *a += 1; };
let mut c2 = || { *a += 10; };
c1();
c2();
println!("value is {v}");
}
error[E0524]: two closures require unique access to `*a` at the same time
--> a2.rs:5:18
|
4 | let mut c1 = || { *a += 1; };
| -- -- first borrow occurs due to use of `*a` in closure
| |
| first closure is constructed here
5 | let mut c2 = || { *a += 10; };
| ^^ -- second borrow occurs due to use of `*a` in closure
| |
| second closure is constructed here
6 | c1();
| -- first borrow later used here
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0524`.
Note what the diagnostic's later-use span points at: c1() on the line after c2's definition. c2 is never called before the error is reported - the conflict is between c2's construction and c1's remaining life, not between two calls. The repair here is the index's other family: when the variable genuinely has to serve two closures that are both live, share it. The reference moves behind Rc<RefCell<...>>, each closure clones or holds one Rc, and at call time each borrows the value mutably for the duration of that call only:
use std::cell::RefCell;
use std::rc::Rc;
fn set(x: &mut isize) { *x += 4; }
fn dragoooon(x: &mut isize) {
let x = Rc::new(RefCell::new(x));
let y = Rc::clone(&x);
let c1 = || { let mut x2 = x.borrow_mut(); set(&mut x2); };
let c2 = || { let mut x2 = y.borrow_mut(); set(&mut x2); };
c2();
c1();
}
fn main() { let mut v = 1; dragoooon(&mut v); println!("value is {v}"); }
value is 9
The output is value is 9: c2 runs first and adds 4, then c1 adds 4, and the caller's variable ends at 9. The trade is explicit in the code - the uniqueness the borrow checker enforced at compile time is now RefCell's runtime borrow check, and two closures that call each other while one holds a borrow_mut would fail at runtime instead. Reach for this repair when both closures must stay alive together; when they need not, the one-at-a-time repairs are simpler.
The third failure: two closures pushing through a &mut Vec
The value behind the reference can be a collection. a is a local &mut Vec binding, one closure captures it to push 3, and a second captures it to push 4:
fn main() {
let mut v = vec![1, 2];
let a = &mut v;
let mut c1 = || { a.push(3); };
let mut c2 = || { a.push(4); };
c1();
c2();
println!("v is {v:?}");
}
error[E0524]: two closures require unique access to `*a` at the same time
--> a3.rs:5:18
|
4 | let mut c1 = || { a.push(3); };
| -- - first borrow occurs due to use of `*a` in closure
| |
| first closure is constructed here
5 | let mut c2 = || { a.push(4); };
| ^^ - second borrow occurs due to use of `*a` in closure
| |
| second closure is constructed here
6 | c1();
| -- first borrow later used here
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0524`.
A method call through a captured reference is still a use of that reference in the closure, so push gives each closure the same unique-access hold as the helper calls in the failures around it. The repair is to take the capture away entirely: both closures receive the vector as a parameter at call time and capture nothing. Between calls, no closure holds anything, so the same a can be handed to each in turn:
fn main() {
let mut v = vec![1, 2];
let a = &mut v;
let c1 = |s: &mut Vec<i32>| { s.push(3); };
let c2 = |s: &mut Vec<i32>| { s.push(4); };
c1(a);
c2(a);
println!("v is {v:?}");
}
v is [1, 2, 3, 4]
The output is v is [1, 2, 3, 4]: both pushes land, in call order. Passing the reference as a parameter is the repair to reach for when the two closures are really two operations over one value - the operations stay separate, and the sharing moves to the call sites, where the borrow checker can see each hand-off end before the next begins.
The fourth failure: two closures passing a &mut struct to a helper
The reference can point at a struct, and both closures can do their work through the same helper. a is a &mut Counter parameter; c1 and c2 each capture it to pass to bump:
struct Counter { n: i32 }
fn bump(c: &mut Counter) { c.n += 1; }
fn foo(a: &mut Counter) {
let mut c1 = || { bump(a) };
let mut c2 = || { bump(a) };
c1();
c2();
}
fn main() { let mut c = Counter { n: 5 }; foo(&mut c); println!("n is {}", c.n); }
error[E0524]: two closures require unique access to `*a` at the same time
--> a4.rs:5:18
|
4 | let mut c1 = || { bump(a) };
| -- - first borrow occurs due to use of `*a` in closure
| |
| first closure is constructed here
5 | let mut c2 = || { bump(a) };
| ^^ - second borrow occurs due to use of `*a` in closure
| |
| second closure is constructed here
6 | c1();
| -- first borrow later used here
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0524`.
Nothing about the struct changes the conflict - the header names *a, the whole place behind the reference, not the field bump touches. The repair, when the two closures always run together, is to stop having two closures: merge the bodies into one closure that performs both operations. One closure needs unique access once, and there is no second hold to conflict with it:
struct Counter { n: i32 }
fn bump(c: &mut Counter) { c.n += 1; }
fn foo(a: &mut Counter) {
let mut both = || { bump(a); bump(a); };
both();
}
fn main() { let mut c = Counter { n: 5 }; foo(&mut c); println!("n is {}", c.n); }
n is 7
The output is n is 7: the counter starts at 5 and the merged closure's two bump calls bring it to 7. Merging is the smallest repair in this guide, and it fits exactly when the two closures were never independently scheduled - if their call sites genuinely differ, the parameter or one-at-a-time repairs keep them separate instead.
The fifth failure: the first closure called again after the second is defined
Calling the first closure before the second exists does not discharge it. Here c1 runs once, c2 is defined, and then c1 is called again - that final call keeps c1's hold live across c2's construction:
fn set(x: &mut i32) { *x += 1; }
fn foo(a: &mut i32) {
let mut c1 = || { set(a) };
c1();
let mut c2 = || { set(a) };
c2();
c1();
}
fn main() { let mut v = 0; foo(&mut v); println!("value is {v}"); }
error[E0524]: two closures require unique access to `*a` at the same time
--> a7.rs:5:18
|
3 | let mut c1 = || { set(a) };
| -- - first borrow occurs due to use of `*a` in closure
| |
| first closure is constructed here
4 | c1();
5 | let mut c2 = || { set(a) };
| ^^ - second borrow occurs due to use of `*a` in closure
| |
| second closure is constructed here
6 | c2();
7 | c1();
| -- first borrow later used here
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0524`.
The later-use span sits on the last line of the function, under the second c1() - it is the whole reason the error fires. The repair is to mean it: finish the first closure before the second is defined. With c1's calls all complete and no later use, its hold ends at its last call, and c2 may capture the same reference - even in the plain form, with no reborrow:
fn set(x: &mut i32) { *x += 1; }
fn foo(a: &mut i32) {
let mut c1 = || { set(a) };
c1();
let mut c2 = || { set(a) };
c2();
}
fn main() { let mut v = 0; foo(&mut v); println!("value is {v}"); }
value is 2
The output is value is 2: each closure adds 1. This is the boundary the whole error turns on - two closures over one reference are fine in sequence and forbidden only while their lifetimes overlap. If the interleaved call pattern is real and the first closure must run again after the second exists, sequencing is not available, and the repair is one of the sharing ones: parameters, a merge, or Rc<RefCell<...>>.
The sixth failure: three closures over one reference
A third closure does not dilute the conflict - it repeats it. Each new closure that needs unique access through a while c1 is still live draws its own E0524, so this program reports the error twice, once for c2 and once for c3:
fn set(x: &mut i32) { *x += 1; }
fn foo(a: &mut i32) {
let mut c1 = || { set(a) };
let mut c2 = || { set(a) };
let mut c3 = || { set(a) };
c1();
c2();
c3();
}
fn main() { let mut v = 0; foo(&mut v); println!("value is {v}"); }
error[E0524]: two closures require unique access to `*a` at the same time
--> a6.rs:4:18
|
3 | let mut c1 = || { set(a) };
| -- - first borrow occurs due to use of `*a` in closure
| |
| first closure is constructed here
4 | let mut c2 = || { set(a) };
| ^^ - second borrow occurs due to use of `*a` in closure
| |
| second closure is constructed here
5 | let mut c3 = || { set(a) };
6 | c1();
| -- first borrow later used here
error[E0524]: two closures require unique access to `*a` at the same time
--> a6.rs:5:18
|
3 | let mut c1 = || { set(a) };
| -- - first borrow occurs due to use of `*a` in closure
| |
| first closure is constructed here
4 | let mut c2 = || { set(a) };
5 | let mut c3 = || { set(a) };
| ^^ - second borrow occurs due to use of `*a` in closure
| |
| second closure is constructed here
6 | c1();
| -- first borrow later used here
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0524`.
The two headers are identical apart from the line they point at, and the aborting line counts both: "2 previous errors". The repair combines the first failure's reborrow with the fifth's sequencing - each closure is scoped in its own block, takes a fresh &mut *a reborrow, and is finished before the next is defined:
fn set(x: &mut i32) { *x += 1; }
fn foo(a: &mut i32) {
{
let mut c1 = || { set(&mut *a) };
c1();
}
{
let mut c2 = || { set(&mut *a) };
c2();
}
let mut c3 = || { set(&mut *a) };
c3();
}
fn main() { let mut v = 0; foo(&mut v); println!("value is {v}"); }
value is 3
The output is value is 3: three closures, three increments, no overlap. The pattern scales in the dull way the error demands - the Nth closure is never the problem; every closure after the first is a problem only while an earlier one is still live.
The fixes in reach order
- Run the closures one at a time, each taking a fresh reborrow (
&mut *x) at its own definition - the error index's second prescription, and the repair for the first and sixth failures. No closure holds the original reference across another's lifetime. - Finish the first closure before the second is defined - the repair for the fifth failure. Once the first closure has no later use, the second may capture the same reference even in the plain form.
- Pass the reference as a parameter to the closures - the repair for the third failure. The closures capture nothing, so the reference may be handed to each at call time in turn.
- Merge the closures into one - the repair for the fourth failure. One closure, one hold, both operations inside it.
- Share the value through
Rc<RefCell<...>>(orArcwhen it runs concurrently) - the error index's first prescription, and the repair for the second failure. Each closure borrows the value briefly at call time; uniqueness becomes a runtime check instead of a compile-time one.
Traps
c2 passes a to show, which takes &isize - c2 needs only shared access, so it does not require unique access, and the conflict is reported as the one-closure error instead (the transcript also carries an unused-mut warning for c2, which a read-only closure does not need):fn set(x: &mut isize) { *x += 4; }
fn show(x: &isize) { println!("seen is {x}"); }
fn foo(a: &mut isize) {
let mut c1 = || { set(a) };
let mut c2 = || { show(a) };
c1();
c2();
}
fn main() { let mut v = 1; foo(&mut v); println!("value is {v}"); }
warning: variable does not need to be mutable
--> a5.rs:5:9
|
5 | let mut c2 = || { show(a) };
| ----^^
| |
| help: remove this `mut`
|
= note: `#[warn(unused_mut)]` (part of `#[warn(unused)]`) on by default
error[E0501]: cannot borrow `*a` as immutable because previous closure requires unique access
--> a5.rs:5:18
|
4 | let mut c1 = || { set(a) };
| -- - first borrow occurs due to use of `*a` in closure
| |
| closure construction occurs here
5 | let mut c2 = || { show(a) };
| ^^ - second borrow occurs due to use of `*a` in closure
| |
| second borrow occurs here
6 | c1();
| -- first borrow later used here
error: aborting due to 1 previous error; 1 warning emitted
For more information about this error, try `rustc --explain E0501`.
The tell is in the spans: where the E0524 transcripts say "first closure is constructed here" and "second closure is constructed here", this one says "closure construction occurs here" and "second borrow occurs here" - the second closure is being read as an ordinary second borrow against the first closure's hold, which is E0501's framing. The repairs rhyme with both guides' - sequence the closures, here with a reborrow in the writer and a shared reborrow in the reader:
fn set(x: &mut isize) { *x += 4; }
fn show(x: &isize) { println!("seen is {x}"); }
fn foo(a: &mut isize) {
let mut c1 = || { set(&mut *a) };
c1();
let c2 = || { show(&*a) };
c2();
}
fn main() { let mut v = 1; foo(&mut v); println!("value is {v}"); }
seen is 5
value is 5
The output is seen is 5, then value is 5: the writer adds 4 first, and the reader sees the result. And the limiting case confirms the condition from the other side - two closures that both only read through a &mut parameter compile as they stand, because neither requires unique access:
fn show(x: &i32) { println!("seen is {x}"); }
fn foo(a: &mut i32) {
let c1 = || { show(a) };
let c2 = || { show(a) };
c1();
c2();
}
fn main() { let mut v = 7; foo(&mut v); println!("value is {v}"); }
seen is 7
seen is 7
value is 7
The same shape over an owned variable - E0499, not E0524. Replace the &mut parameter with an owned local and the error changes code. Two closures capturing the owned x mutably draw the ordinary second-mutable-borrow header:
fn main() {
let mut x = 5;
let mut c1 = || { x += 1; };
let mut c2 = || { x += 10; };
c1();
c2();
println!("value is {x}");
}
error[E0499]: cannot borrow `x` as mutable more than once at a time
--> n1_owned.rs:4:18
|
3 | let mut c1 = || { x += 1; };
| -- - first borrow occurs due to use of `x` in closure
| |
| first mutable borrow occurs here
4 | let mut c2 = || { x += 10; };
| ^^ - second borrow occurs due to use of `x` in closure
| |
| second mutable borrow occurs here
5 | c1();
| -- first borrow later used here
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0499`.
And the same holds for an owned vector both closures push to:
fn main() {
let mut v = vec![1, 2];
let mut c1 = || { v.push(3); };
let mut c2 = || { v.push(4); };
c1();
c2();
println!("v is {v:?}");
}
error[E0499]: cannot borrow `v` as mutable more than once at a time
--> n3_owned_vec.rs:4:18
|
3 | let mut c1 = || { v.push(3); };
| -- - first borrow occurs due to use of `v` in closure
| |
| first mutable borrow occurs here
4 | let mut c2 = || { v.push(4); };
| ^^ - second borrow occurs due to use of `v` in closure
| |
| second mutable borrow occurs here
5 | c1();
| -- first borrow later used here
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0499`.
The distinction matters because the repairs differ in emphasis: an E0499 between two closures is repaired by reordering or restructuring ordinary mutable borrows of a value the function owns; an E0524 is specifically about a reference that was lent onward into two closures, and the reborrow, parameter, and Rc<RefCell<...>> repairs - changing what the closures hold - are on the table. Check what the closures captured before choosing: a reference they were handed, or a value they borrow directly.
No closures at all - E0499 again. The conflict with both closures removed is two named mutable references, and it takes the same neighboring code:
fn main() {
let mut x = 5;
let m1 = &mut x;
let m2 = &mut x;
*m1 += 1;
*m2 += 10;
println!("value is {x}");
}
error[E0499]: cannot borrow `x` as mutable more than once at a time
--> b4_direct_borrows.rs:4:14
|
3 | let m1 = &mut x;
| ------ first mutable borrow occurs here
4 | let m2 = &mut x;
| ^^^^^^ second mutable borrow occurs here
5 | *m1 += 1;
| -------- first borrow later used here
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0499`.
E0524 is not the code for "borrowed as mutable more than once" in general - it is the code for the two-closure case of that conflict, where both holds are closures capturing a reference. A search that lands on this guide from a plain two-reference error is in E0499's territory.
One closure plus an outside use - E0501. A single closure capturing the reference, with the conflicting act outside any closure, is the neighboring one-closure error:
fn inside_closure(x: &mut i32) { *x += 1; }
fn outside_closure(x: &mut i32) { *x += 10; }
fn foo(a: &mut i32) {
let mut bar = || { inside_closure(a) };
outside_closure(a);
bar();
}
fn main() { let mut v = 1; foo(&mut v); println!("value is {v}"); }
error[E0501]: cannot borrow `*a` as mutable because previous closure requires unique access
--> n2_one_closure_outside.rs:5:21
|
4 | let mut bar = || { inside_closure(a) };
| -- - first borrow occurs due to use of `*a` in closure
| |
| closure construction occurs here
5 | outside_closure(a);
| ^ second borrow occurs here
6 | bar();
| --- first borrow later used here
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0501`.
Count the closures before applying this guide's repairs: E0524 needs two of them, both requiring unique access through the same reference. With one closure, the conflict is between the closure and the code around it, and the framing - and the header - is E0501's.
The second closure never called - still E0524. Deleting the second closure's calls does not rescue the program. c2 is defined, never used, and warned about as unused - and the error fires anyway, because c2's construction is itself the second hold, and c1() afterward keeps the first hold live across it:
fn set(x: &mut i32) { *x += 1; }
fn foo(a: &mut i32) {
let mut c1 = || { set(a) };
let c2 = || { set(a) };
c1();
println!("c2 defined but never called");
}
fn main() { let mut v = 0; foo(&mut v); println!("value is {v}"); }
error[E0524]: two closures require unique access to `*a` at the same time
--> b2_second_never_called.rs:4:14
|
3 | let mut c1 = || { set(a) };
| -- - first borrow occurs due to use of `*a` in closure
| |
| first closure is constructed here
4 | let c2 = || { set(a) };
| ^^ - second borrow occurs due to use of `*a` in closure
| |
| second closure is constructed here
5 | c1();
| -- first borrow later used here
warning: unused variable: `c2`
--> b2_second_never_called.rs:4:9
|
4 | let c2 = || { set(a) };
| ^^ help: if this is intentional, prefix it with an underscore: `_c2`
|
= note: `#[warn(unused_variables)]` (part of `#[warn(unused)]`) on by default
error: aborting due to 1 previous error; 1 warning emitted
For more information about this error, try `rustc --explain E0524`.
The practical reading cuts the other way from the one-closure case: there, a closure that is never called keeps nothing live; here, a closure that is never called still counts as a holder from the moment it is constructed. If a second closure over the same reference exists in the code at all while the first is still used later, expect this error - the fix is to remove the second closure, finish the first first, or change what one of them holds.
Borrowed data escaping a closure - E0521. A closure problem with a similar shape but a different defect: here a closure parameter annotated &str is stored into a vector declared outside the closure, and the borrowed data escapes:
fn main() {
let mut list: Vec<&str> = Vec::new();
let _add = |el: &str| { list.push(el); };
}
error[E0521]: borrowed data escapes outside of closure
--> n4_escapes.rs:3:29
|
2 | let mut list: Vec<&str> = Vec::new();
| -------- `list` declared here, outside of the closure body
3 | let _add = |el: &str| { list.push(el); };
| -- ^^^^^^^^^^^^^ `el` escapes the closure body here
| |
| `el` is a reference that is only valid in the closure body
|
= note: requirement occurs because of a mutable reference to `Vec<&str>`
= note: mutable references are invariant over their type parameter
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0521`.
Nothing is shared between two closures here - there is only one closure, and the problem is where its parameter's data goes. The header's verb is the tell: "escapes", not "require unique access".
One closure, called twice - compiles. A single closure needing unique access is no conflict at all, however often it runs:
fn set(x: &mut i32) { *x += 1; }
fn foo(a: &mut i32) {
let mut c1 = || { set(a) };
c1();
c1();
}
fn main() { let mut v = 0; foo(&mut v); println!("value is {v}"); }
value is 2
The output is value is 2. This is the control case behind the merge repair: operations that can live in one closure never meet this error.
Two closures over a shared reference - compiles. Unique access is the trigger, and shared access is shareable: two closures capturing the same & binding, both reading, coexist freely:
fn main() {
let x = 7;
let r = &x;
let c1 = || { println!("c1 sees {r}"); };
let c2 = || { println!("c2 sees {r}"); };
c1();
c2();
}
c1 sees 7
c2 sees 7
The output is c1 sees 7, then c2 sees 7. Any number of read-only closures may hold a shared reference at the same time; the error begins only when the access each closure needs is unique.
Two closures over two different references - compiles. The conflict is per-reference, not per-function: c1 holds a, c2 holds b, and the two references point at different variables:
fn set(x: &mut i32) { *x += 1; }
fn main() {
let mut p = 1;
let mut q = 10;
let a = &mut p;
let b = &mut q;
let mut c1 = || { set(a) };
let mut c2 = || { set(b) };
c1();
c2();
println!("p is {p}, q is {q}");
}
p is 2, q is 11
The output is p is 2, q is 11. When a program can be split so each closure gets its own reference to its own value, there is nothing to repair.
A second closure over a copy - compiles. If the second closure captures a snapshot taken before either closure exists, it touches nothing the first closure holds:
fn set(x: &mut i32) { *x += 1; }
fn foo(a: &mut i32) {
let snapshot = *a;
let mut c1 = || { set(a) };
let c2 = || { println!("snapshot is {snapshot}"); };
c1();
c2();
}
fn main() { let mut v = 3; foo(&mut v); println!("value is {v}"); }
snapshot is 3
value is 4
The output is snapshot is 3, then value is 4: the copy preserves the value as it was, and the mutating closure's increment lands independently. The trade is the snapshot's usual one - the second closure does not observe the first's writes - which is exactly right when what it needed was the earlier value.
Decision summary
- The header is E0524 when three facts hold together: two closures are live at the same time, both capture the same variable, that variable is itself a reference (
&mutparameter or local&mutbinding), and both closures need unique access through it. - The spans are the two holds: first closure constructed, second closure constructed, and a later call of the first proving the overlap. The error fires at the second closure's definition, before either closure runs.
- A second closure that is never called still triggers the error; a first closure whose last use precedes the second's definition does not - in sequence, the same two closures compile.
- Neighbors ruled out by the header before applying this guide's repairs: E0499 for the same shapes over an owned variable and for any closure-free pair of mutable borrows, E0501 for one closure plus an outside use and for a read-only second closure, E0521 for borrowed data escaping a closure.
- Not an error at all: one closure called any number of times, two closures that only read, two closures over different references, and a second closure that captures a copy instead of the reference.
Checklist
- Count the closures and what each captured: two live closures over one handed-on reference means E0524 repairs; an owned variable means E0499 repairs; one closure means E0501 repairs.
- Find the first closure's last call; the second closure may be defined only after that line, unless what it holds changes.
- Decide whether both closures must stay alive together: if not, sequence them - reborrow in each, or finish the first before defining the second. If so, change the sharing - parameters at call time, one merged closure, or
Rc<RefCell<...>>. - Do not soften the second closure to make the error go away unless reading is genuinely all it does - a read-only second closure changes the error to E0501, it does not remove the conflict.
- Do not rely on the second closure being unused - its construction alone is the second hold, and the error fires whether or not it is ever called.
Related guides on this site
- E0500 Closure Unique Access Fix
- E0610 Field Access on a Primitive Fix
- Fix Rust E0499 "cannot borrow as mutable more than once at a time" — Two Bindings, a Function Call, Two Vec Elements, and a Method Call
- E0091 Unused Type Parameter Fix
Sources
- Rust, Error codes index: E0524 - the definition of the error: a variable which requires unique access is being used in more than one closure at the same time - with the
&mut isizetwo-closure example this guide's first failure follows, and the two prescriptions this guide follows: use reference counted types such asRc(orArcif it runs concurrently), here paired withRefCell, when the variable must serve more than one closure at a time, or run the closures one at a time, each reborrowing with&mut *x. - The Rust Programming Language, References and Borrowing - the borrow rules underneath: one mutable reference or any number of immutable references to a value, never both at once; and the scope rule the sequencing repairs rely on - a reference's scope runs from where it is introduced through the last time it is used, so holds that do not overlap in use do not conflict.
- Rust, Error codes index: E0499 - the neighboring error separated in the traps: a variable was borrowed as mutable more than once at a time - the code for the same two-closure shapes over an owned variable and for closure-free pairs of mutable borrows.
- Rust, Error codes index: E0501 - the neighboring error separated in the traps: a mutable variable is used but it is already captured by a closure - the code for one closure plus an outside use, and for a second closure that only reads through the captured reference.
- Rust, Error codes index: E0521 - the neighboring error separated in the traps: borrowed data escapes outside of a closure.
Keep Exploring
The full Rust hub \u2014 ownership, structs and enums, async, and measured comparisons.
Every guide on the site in one list, grouped by topic.
Every browser-only checker and verifier on the site.
The C++, Python, Sysadmin, Rust, Go, and TypeScript hubs.