# How do I fix "closure may outlive the current function" with thread::spawn (E0373)?
Source: https://ruststudio.org/community/t/143/how-do-i-fix-closure-may-outlive-the-current-function-with-thread-spawn-e0373
Author: Ruststudio Team (staff) | Published: 2026-12-05 | Updated: 2026-12-05 | Checked with: rustc 1.93.0, Windows 11
Category: Help and questions | Kind: FAQ | Tags: errors, threads, closures | Views: 0

## Short answer
A spawned thread can run longer than the function that started it, so it cannot borrow that function's variables. Add move before the closure so the thread owns what it uses. To share one value between threads, use Arc and clone it per thread. Scoped threads may borrow, because they end before the function does.

## Answer
## Answer
`thread::spawn` needs a closure that is valid for the whole life of the thread, which the compiler cannot bound. A closure that merely borrows `name` could be running after `name` is freed. Ownership has to move into the thread.

```rust title=src/main.rs
use std::thread;

fn main() {
    let name = String::from("Ada");
    let handle = thread::spawn(|| {
        println!("hello, {name}");
    });
    handle.join().unwrap();
}
```

```text
error[E0373]: closure may outlive the current function, but it borrows `name`, which is owned by the current function
 --> src/main.rs:5:32
  |
5 |     let handle = thread::spawn(|| {
  |                                ^^ may outlive borrowed value `name`
6 |         println!("hello, {name}");
  |                           ---- `name` is borrowed here
  |
note: function requires argument type to outlive `'static`
 --> src/main.rs:5:18
  |
5 |       let handle = thread::spawn(|| {
  |  __________________^
6 | |         println!("hello, {name}");
7 | |     });
  | |______^
help: to force the closure to take ownership of `name` (and any other referenced variables), use the `move` keyword
  |
5 |     let handle = thread::spawn(move || {
  |                                ++++

error: aborting due to 1 previous error

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

The answers below are the valid ways out.

## Answers (3)

### Answer 1 by Ruststudio Team (staff), 2026-12-05, 0 votes
## Approach 1: move the value in
The compiler's own suggestion. After `move`, the closure owns `name` and the main function can no longer use it.

```rust title=src/main.rs
use std::thread;

fn main() {
    let name = String::from("Ada");
    let handle = thread::spawn(move || {
        println!("hello, {name}");
    });
    handle.join().unwrap();
}
```

```text
hello, Ada
```

### Answer 2 by Ruststudio Team (staff), 2026-12-05, 0 votes
## Approach 2: share with Arc
When several threads need the same data, put it in an `Arc` and give each thread its own clone of the pointer: `let name = Arc::clone(&name);` before the `move` closure. Add a `Mutex` inside the `Arc` if they must change it. See the thread on Box, Rc, Arc and Mutex.

### Answer 3 by Ruststudio Team (staff), 2026-12-05, 0 votes
## Approach 3: scoped threads
`std::thread::scope` guarantees that every thread started inside it ends before the scope does, so these threads may borrow local variables with no `move` and no `Arc`:

```rust
let name = String::from("Ada");
std::thread::scope(|s| {
    s.spawn(|| println!("hello, {name}"));
});
println!("{name} is still ours");
```

Best when: the work is a fork and join inside one function.

## Common mistakes
- Adding `move` and then using the variable afterwards ("borrow of moved value"). Clone it first or use `Arc`.
- Forgetting `handle.join()`. Without it, `main` may end before the thread prints anything.
