error[E0373]: closure may outlive the current function, but it borrows `x`, which is owned by the current function (Rust) — how to fix
error[E0373]: closure may outlive the current function, but it borrows `data`, which is owned by the current function
--> src/main.rs:6:32
|
6 | let handle = thread::spawn(|| {
| ^^ may outlive borrowed value `data`
7 | println!("{:?}", data);
| ---- `data` is borrowed here
|
help: to force the closure to take ownership of `data` (and any other referenced variables), use the `move` keyword
|
6 | let handle = thread::spawn(move || {
| ++++
Quick fix: Add move so the closure owns its captures: thread::spawn(move || ...). If you still need the value afterwards, clone it before the closure (let d = data.clone();) or share it with Arc (let d = Arc::clone(&data);). For threads that finish before the function returns, use std::thread::scope and borrow normally.
What it means
By default a closure captures variables by reference when that's enough. That's fine if the closure runs and is dropped inside the function. But thread::spawn, tokio::spawn and returned closures require 'static: the closure may keep running after the function ends, when its locals are already gone. A reference to a local would dangle, so Rust rejects it.
use std::thread;
fn main() {
let data = vec![1, 2, 3];
let handle = thread::spawn(|| {
println!("{:?}", data); // borrows `data`
});
handle.join().unwrap();
}
Fix 1: move the values into the closure
The compiler's suggestion is usually right:
let handle = thread::spawn(move || {
println!("{:?}", data);
});
Now the closure owns data. After this point data can't be used in main (unless its type is Copy, in which case a copy is moved in).
Fix 2: clone or Arc when you still need the value
If the outer function also needs the value, or several threads need it, give the closure its own handle:
use std::sync::Arc;
use std::thread;
let data = Arc::new(vec![1, 2, 3]);
let mut handles = Vec::new();
for i in 0..4 {
let data = Arc::clone(&data);
handles.push(thread::spawn(move || {
println!("thread {i}: {:?}", data);
}));
}
for h in handles { h.join().unwrap(); }
println!("still usable: {:?}", data);
For mutation across threads use Arc<Mutex<T>> (or RwLock, atomics).
For cheap values a plain .clone() before the closure is enough:
let name_for_thread = name.clone();
thread::spawn(move || println!("{name_for_thread}"));
Fix 3: scoped threads (borrow without move)
If all threads finish before the function returns, std::thread::scope (stable since Rust 1.63) lets them borrow locals directly:
let data = vec![1, 2, 3];
let mut total = 0;
std::thread::scope(|s| {
s.spawn(|| println!("{:?}", data));
s.spawn(|| total = 10);
});
// all scoped threads are joined here
println!("{total}");
Fix 4: async tasks
tokio::spawn has the same 'static requirement:
let client = client.clone();
let url = url.to_string();
tokio::spawn(async move {
client.get(&url).send().await
});
Note both move and owned data: an async move block that captures a &str borrowed from a local still fails. Convert to an owned String, or share with Arc.
Fix 5: returning a closure
fn make_adder(n: i32) -> impl Fn(i32) -> i32 {
move |x| x + n // without `move`, `n` would be borrowed and dropped
}
For Box<dyn Fn()> the same applies: use move and capture owned values.
Last updated 2026-11-29.