Rust

E0373 Closure May Outlive Fix

The closure escapes - it is returned, spawned onto a thread, or handed to a function that requires 'static - and it still borrows a local the current function owns. This guide separates four shapes that produce E0373 and matches each one to the same repair in four positions: move, written before the closure's parameter list, so the closure owns its captures instead of borrowing them.

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

By the end of this guide
  • The rule underneath: closures capture by reference by default, move takes ownership
  • How to read the diagnostic
  • The first failure: a closure returned in a Box
  • The second failure: a closure returned as impl Fn

Written November 24, 2026

Tested with Rust 1.99.1

What this guide is

Error E0373 is a closure that may outlive the current function while borrowing a value that function owns. The error codes index states the cause in one line: a captured variable in a closure may not live long enough. It explains that the error occurs when data captured by a closure is used when that data may no longer exist, and that it is most commonly seen when attempting to return a closure.

The four forms in this guide each reproduce E0373 with a small, complete program, and the closure escapes by a different route in each one. A closure returned inside a Box<dyn Fn>. A closure returned as an impl Fn. A closure handed to std::thread::spawn. And a closure passed to a function whose argument must outlive 'static. In each family the failing program is small, the full compiler output is shown, and the fix is the same word in the same position: move, written before the closure, so the closure takes ownership of the variable it uses instead of borrowing it.

Four neighboring shapes are not E0373 and are not in the families. A value dropped while a plain reference to it is still used is E0597. Data borrowed from a closure's own parameter escaping into a captured variable is E0521. A closure that needs unique access to a value that is already borrowed is E0500, and a variable borrowed as mutable more than once is E0499. The traps section shows each one with its real code. One further program in the traps borrows a reference parameter under a named lifetime and compiles with no move at all, because nothing it captures is owned by the function that creates the closure.

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: closures capture by reference by default, move takes ownership

Closures can capture values from the scope in which they are defined, and they capture in the least demanding way the body allows. The error codes index puts the default plainly: by default, Rust captures closed-over data by reference. The captured local in the first family is stack-allocated by the function that creates the closure, so once that function returns, the local no longer exists - and a returned closure still holding a reference to it would be free to read a value that is gone.

The Rust Book chapter on closures describes the three ways a closure can capture a value, which map to the three ways a function can take a parameter: borrowing immutably, borrowing mutably, and taking ownership. The body decides which capture happens: a body that only reads a variable borrows it, a body that changes it borrows it mutably, and a body that consumes it takes ownership. Nothing about calling and dropping a closure like these - whose bodies only read the captured value - inside one function forces ownership, though a body that consumes a captured value takes ownership wherever the closure lives - which is why the same read-only closure text compiles in place and fails the moment it escapes.

The move keyword changes the capture itself. The Book states that if you want to force the closure to take ownership of the values it uses in the environment, even though the body of the closure doesn't strictly need ownership, you can use the move keyword before the parameter list. The index gives the same solution for this error: switch to using a move closure, which moves (or copies, where possible) data into the closure rather than taking references to it, so the closure has its own copy of the data. move is not free in one respect: a variable moved into a closure is no longer available to the code that defined it, under the ordinary ownership rules. For a copyable value such as an integer the capture is a copy and the original stays usable; for a String the original is moved.

A thread is the same rule seen from the other side. The Book's thread example explains that if the main thread maintained ownership of a value but ended before the new thread and dropped it, the immutable reference in the thread would be invalid, so the compiler requires the value to be moved into the closure given to the new thread. The index adds the point readers most often argue with: calling join inside the function does not rescue a borrowing closure, because even though join blocks until the thread has completed, the compiler cannot prove that this behavior is safe, and so won't let it happen. The requirement attaches to thread::spawn's signature, not to how the handle is used afterward.

The escape hatch that is not a loophole is a lifetime. E0373 is about a value owned by the current function. A closure that captures a reference the function itself received can outlive the function exactly as long as that reference does, and a named lifetime on the parameter and on the returned closure says so: the traps section ends with a program that returns a closure borrowing its parameter, compiles, and runs - with no move, because there is no local for the closure to outlive.

How to read the diagnostic

The header names both halves of the problem: "closure may outlive the current function, but it borrows" the variable, "which is owned by the current function". The first span marks the closure itself with the label "may outlive borrowed value", and a marker on the variable inside the body reads "x is borrowed here" - the name in those labels is the capture to fix. Below the spans, a note names the escape route: "closure is returned here" when the closure is a return value, and "function requires argument type to outlive 'static" when the closure is an argument to thread::spawn or to a function with a 'static bound. That note is the quickest way to tell which family a new report belongs to.

Read the help next: "to force the closure to take ownership of" the variable "(and any other referenced variables), use the move keyword" is present in every family in this guide, and the help rewrites the closure line with move inserted and marked with ++++, in front of the parameter list. Following that rewrite is the whole fix in all four families. Where that help is absent, the code is a different one: the E0597, E0521, E0500, and E0499 reports in the traps carry no move help, because ownership of a capture is not their missing piece.

The first failure: a closure returned in a Box

The function creates a local, builds a closure that reads it, and returns the closure boxed as a trait object. The caller can invoke the closure long after the function has returned, so the borrow cannot be allowed to leave:

Rust
fn make_adder() -> Box<dyn Fn(i32) -> i32> {
    let x = 10;
    Box::new(|y| x + y)
}

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

error: aborting due to 1 previous error

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

The span under the closure reads "may outlive borrowed value x", and the note below it - "closure is returned here" - names the escape. The help rewrites the Box::new line with move in front of the closure:

Rust
fn make_adder() -> Box<dyn Fn(i32) -> i32> {
    let x = 10;
    Box::new(move |y| x + y)
}

fn main() {
    let add = make_adder();
    println!("{}", add(5));
}
Terminal
$ rustc --edition 2021 fix_returned.rs -o fix_returned
$ ./fix_returned
15

The output is 15: the closure added its captured 10 to the argument 5. With move, the value 10 - an integer, so a copy - belongs to the closure, and nothing refers back to the finished function's frame. The caller's side did not change at all; only the capture did.

The second failure: a closure returned as impl Fn

Returning the closure directly, as an impl Fn, escapes in exactly the same way - the concrete closure type is hidden, but its lifetime is not extended by hiding it. Here the captured value is a String, so the borrow is the only thing keeping the closure's body meaningful after the return:

Rust
fn make_greeting() -> impl Fn() -> String {
    let name = String::from("Ferris");
    || format!("Hello, {}!", name)
}

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

error: aborting due to 1 previous error

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

The header names name, and the help again rewrites the closure line, this time at the start of the return expression. Follow it exactly:

Rust
fn make_greeting() -> impl Fn() -> String {
    let name = String::from("Ferris");
    move || format!("Hello, {}!", name)
}

fn main() {
    let greet = make_greeting();
    println!("{}", greet());
}
Terminal
$ rustc --edition 2021 fix_impl.rs -o fix_impl
$ ./fix_impl
Hello, Ferris!

The output is the greeting, Hello, Ferris!. Note the cost this time: name is a String, which does not copy, so move transfers ownership of the string into the closure and the function could not use name again after the closure is created. That is the intended trade - the value has to live wherever the closure lives, and after the return that is with the caller. If the function still needs the text afterward, clone it before the closure and move the clone in.

The third failure: a closure passed to a thread

Nothing is returned here. The closure escapes sideways, onto a new thread that the compiler must assume can run after the current function ends - thread::spawn requires its argument to outlive 'static, which a borrow of a local cannot do:

Rust
fn main() {
    let message = String::from("hello from thread");
    let handle = std::thread::spawn(|| {
        println!("{}", message);
    });
    handle.join().unwrap();
}
Terminal
$ rustc --edition 2021 fail_thread.rs -o fail_thread
error[E0373]: closure may outlive the current function, but it borrows `message`, which is owned by the current function
 --> fail_thread.rs:3:37
  |
3 |     let handle = std::thread::spawn(|| {
  |                                     ^^ may outlive borrowed value `message`
4 |         println!("{}", message);
  |                        ------- `message` is borrowed here
  |
note: function requires argument type to outlive `'static`
 --> fail_thread.rs:3:18
  |
3 |       let handle = std::thread::spawn(|| {
  |  __________________^
4 | |         println!("{}", message);
5 | |     });
  | |______^
help: to force the closure to take ownership of `message` (and any other referenced variables), use the `move` keyword
  |
3 |     let handle = std::thread::spawn(move || {
  |                                     ++++

error: aborting due to 1 previous error

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

The note is the tell for this family: "function requires argument type to outlive 'static", pointing at the spawn call. The join on the next line changes nothing - the requirement is in spawn's signature, and the compiler will not reason from the join back to the safety of the borrow. The help inserts move into the spawn argument:

Rust
fn main() {
    let message = String::from("hello from thread");
    let handle = std::thread::spawn(move || {
        println!("{}", message);
    });
    handle.join().unwrap();
}
Terminal
$ rustc --edition 2021 fix_thread.rs -o fix_thread
$ ./fix_thread
hello from thread

The output is the message, printed by the spawned thread and waited for by the join. As in the impl Fn family, the String is moved: after the spawn call, message belongs to the thread's closure, and main cannot print it again. Code that needs the value on both sides of a spawn clones it first and moves one copy in.

The fourth failure: a closure passed to a 'static argument

No thread and no return value: a plain function declares that its argument must outlive 'static, and that bound alone is enough to make a borrowing closure an E0373. Any API with this shape - task spawners, callback registries, stored handlers - draws the same report:

Rust
fn run<F: Fn() + 'static>(f: F) {
    f();
}

fn main() {
    let x = 42;
    run(|| println!("{}", x));
}
Terminal
$ rustc --edition 2021 fail_static.rs -o fail_static
error[E0373]: closure may outlive the current function, but it borrows `x`, which is owned by the current function
 --> fail_static.rs:7:9
  |
7 |     run(|| println!("{}", x));
  |         ^^                - `x` is borrowed here
  |         |
  |         may outlive borrowed value `x`
  |
note: function requires argument type to outlive `'static`
 --> fail_static.rs:7:5
  |
7 |     run(|| println!("{}", x));
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^
help: to force the closure to take ownership of `x` (and any other referenced variables), use the `move` keyword
  |
7 |     run(move || println!("{}", x));
  |         ++++

error: aborting due to 1 previous error

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

The span marks the closure at the call site, the marker names the borrow of x, and the note repeats the 'static requirement, pointing at the whole run call. The repair is again the one word, at the call site rather than inside run - the bound in run's signature is a promise its callers must meet, not a defect to soften:

Rust
fn run<F: Fn() + 'static>(f: F) {
    f();
}

fn main() {
    let x = 42;
    run(move || println!("{}", x));
}
Terminal
$ rustc --edition 2021 fix_static.rs -o fix_static
$ ./fix_static
42

The output is 42, printed by the closure when run invokes it. Weakening the bound instead - dropping 'static and giving the closure a shorter named lifetime - is the right repair only when the API genuinely never keeps the closure beyond the caller's frame; where the closure is stored or sent elsewhere, the bound is honest and move is the fix.

The fixes in reach order

  • The note says "closure is returned here": write move before the returned closure, whether it is boxed as Box<dyn Fn> or returned as impl Fn.
  • The note says "function requires argument type to outlive 'static": write move before the closure at the spawn or call site - adding a join, or calling the closure promptly, does not change the requirement.
  • The captured value is a String or another non-copying type and the defining code still needs it afterward: clone it before the closure and move the clone in, rather than moving the original.
  • The closure captures only a reference the function itself received, and it may outlive the function exactly as long as that reference: keep the borrow and name the lifetime on the parameter and the returned closure, as in the last trap - no move is needed.
  • The report is E0597, E0521, E0500, or E0499 instead: the closure's capture ownership is not the problem - see the traps.

Traps

  • A value dropped while it is still borrowed is E0597, not E0373 - and no closure is involved at all. A reference is stored out of the block that owns its value, and the error codes index gives the cause: a value was dropped while it was still borrowed. Adding move has nothing to attach to here; the repair is to keep the value alive at least as long as the reference is used, or to stop using the reference after the block:
Rust
fn main() {
    let r;
    {
        let x = 5;
        r = &x;
    }
    println!("{}", r);
}
Terminal
$ rustc --edition 2021 probe_scope.rs -o probe_scope
error[E0597]: `x` does not live long enough
 --> probe_scope.rs:5:13
  |
4 |         let x = 5;
  |             - binding `x` declared here
5 |         r = &x;
  |             ^^ borrowed value does not live long enough
6 |     }
  |     - `x` dropped here while still borrowed
7 |     println!("{}", r);
  |                    - borrow later used here

error: aborting due to 1 previous error

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

Trap
The same code appears when the escaping value is a closure stored in a struct whose field is a Box<dyn Fn() -> i32>: that type carries an implicit 'static lifetime, and a closure borrowing a local stored in it is likewise reported as E0597, "x does not live long enough", with a note naming the object lifetime default - read the code, not the shape of the program. - Borrowed data escaping outside of a closure is E0521. The direction is reversed from E0373: here a reference that is only valid inside the closure body - its own annotated parameter - is pushed into a variable captured from outside, so the short-lived reference escapes. The error codes index defines the code with exactly this shape, and notes that a type annotation on a closure parameter implies a new lifetime declaration; dropping the annotation and letting the compiler infer the parameter's lifetime is its suggested direction:
Rust
fn main() {
    let mut list: Vec<&str> = Vec::new();
    let mut add = |el: &str| {
        list.push(el);
    };
    add("hello");
    println!("{:?}", list);
}
Terminal
$ rustc --edition 2021 probe_escape.rs -o probe_escape
error[E0521]: borrowed data escapes outside of closure
 --> probe_escape.rs:4:9
  |
2 |     let mut list: Vec<&str> = Vec::new();
  |         -------- `list` declared here, outside of the closure body
3 |     let mut add = |el: &str| {
  |                    -- `el` is a reference that is only valid in the closure body
4 |         list.push(el);
  |         ^^^^^^^^^^^^^ `el` escapes the closure body here
  |
  = 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`.

No move repairs this one, because the problem is not what the closure captures but what it lets out: a parameter reference cannot be stored anywhere that outlives the call. - A closure that requires unique access to a value that is already borrowed is E0500. The closure below assigns through a &mut parameter while a second reference derived from that same parameter is still live and used afterward. The error codes index states the cause: the value is already borrowed by the other reference, so it cannot be borrowed by the closure at the same time, and its repair is to create the closure after the borrow has ended:

Rust
fn show(jon_snow: &mut i32) {
    let nights_watch = &*jon_snow;
    let mut starks = || {
        *jon_snow = 3;
    };
    starks();
    println!("{}", nights_watch);
}

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

error: aborting due to 1 previous error

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

A simpler version of this shape - a closure assigning to a local variable while an immutable reference to that variable is still used later - is reported as E0502, cannot borrow as mutable because it is also borrowed as immutable, not E0500: E0500 is specifically the closure-construction conflict the index describes. Either way, ending the earlier borrow before the closure is created is the repair; move does not end a borrow. - A variable borrowed as mutable more than once at a time is E0499. Two &mut borrows of the same variable are both live at the use that follows, and the error codes index states the rule behind it: you can either have many immutable references, or one mutable reference:

Rust
fn main() {
    let mut x = 5;
    let a = &mut x;
    let b = &mut x;
    println!("{} {}", a, b);
}
Terminal
$ rustc --edition 2021 probe_twice.rs -o probe_twice
error[E0499]: cannot borrow `x` as mutable more than once at a time
 --> probe_twice.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`.

There is no closure in this program and no escape: the second borrow itself is the whole error. Use the first borrow last before creating the second, or scope it in a block, and the same code compiles. - A closure borrowing a reference parameter with a named lifetime compiles fine, with no move. Nothing the closure captures is owned by the function that creates it: the closure borrows the &i32 the function received, and the + 'a on the return type promises the closure lives no longer than that reference does. This is the boundary of E0373 - the error starts only when the borrowed value is the current function's own:

Rust
fn make_reader<'a>(x: &'a i32) -> impl Fn() -> i32 + 'a {
    || *x
}

fn main() {
    let value = 9;
    let read = make_reader(&value);
    println!("{}", read());
}
Terminal
$ rustc --edition 2021 fix_named.rs -o fix_named
$ ./fix_named
9

The output is 9, the value behind the reference. Reach for this form when the data genuinely belongs to the caller and the closure is a view over it; reach for move when the closure must own what it uses.

Checklist

  • The header read for both halves: the closure may outlive the current function, and the named variable is owned by that function.
  • The note followed to the escape route: "closure is returned here" for a return value, "function requires argument type to outlive 'static" for a spawn or a 'static argument.
  • move written before the closure's parameter list, in the position the help rewrites - not on the variable's declaration.
  • A moved non-copying value checked for later use in the defining code; where both sides need it, a clone moved in instead of the original.
  • A join, or a prompt call, not mistaken for a repair: the 'static requirement sits in the callee's signature.
  • A closure capturing only a received reference given a named lifetime instead of move, where the caller owns the data.
  • A report of E0597, E0521, E0500, or E0499 repaired by its own rule - keep the value alive, keep parameter references inside the call, end the earlier borrow, or use one mutable borrow at a time - not by adding move.
  • The fixed program run, and its output checked against what the failing program was trying to compute.

Sources

  • Rust, Error codes index: E0373 - a captured variable in a closure may not live long enough; the error occurs when data captured by a closure is used when that data may no longer exist, most commonly when returning a closure; by default Rust captures closed-over data by reference, so a stack-allocated local is gone once its function returns; spawning threads draws the same error, and calling join does not rescue it because the compiler cannot prove the behavior safe; the solution is usually a move closure, which moves (or copies, where possible) data into the closure so it has its own copy.
  • The Rust Programming Language, Closures - closures capture values from their environment in three ways matching how a function takes a parameter: borrowing immutably, borrowing mutably, and taking ownership, decided by what the body does with the captured values; the move keyword before the parameter list forces the closure to take ownership even though the body doesn't strictly need it, which is mostly useful when passing a closure to a new thread; and if the main thread kept ownership of a value and dropped it before the new thread finished, the reference in the thread would be invalid, so the compiler requires the value to be moved into the thread's closure.
  • Rust, Error codes index: E0597 - a value was dropped while it was still borrowed; in its example a value assigned into a longer-lived holder inside a block is dropped at the end of that block while the holder still borrows it, and removing the scope so the value is not dropped until after the last use is the fix.
  • Rust, Error codes index: E0521 - borrowed data escapes outside of a closure; a type annotation on a closure parameter implies a new lifetime declaration, and dropping the annotation so the compiler infers the lifetime is the suggested direction.
  • Rust, Error codes index: E0500 - a borrowed variable was used by a closure; the value is already borrowed by another reference, so it cannot be borrowed by the closure at the same time, and creating the closure after the borrow has ended is the fix.
  • Rust, Error codes index: E0499 - a variable was borrowed as mutable more than once; in Rust you can either have many immutable references, or one mutable reference.

Keep Exploring