Rust error E0521 when a method passes self to a thread or a Tokio task
Fix it fast
Most common cause, 1 of 2std::thread::spawn is called with a closure that uses self
If the method can wait for the work, spawn inside std::thread::scope: scoped threads may borrow non-'static data because they are all joined before scope returns.
impl Worker {
fn run(&self) {
std::thread::scope(|s| {
s.spawn(|| {
println!("working on {}", self.name);
});
});
}
fn run_detached(&self) {
let name = self.name.clone();
std::thread::spawn(move || println!("working on {name}"));
}
}Short answer
Rust reports error E0521, borrowed data escapes outside of method, when a closure or async block using &self goes to std::thread::spawn or tokio::spawn, which both require 'static data. Spawn inside std::thread::scope when the method can wait, or move in clones of what the work needs, with shared state behind Arc. Applies to Rust 1.100 and Tokio 1.54.
Which cause do you have?
Print the full diagnostic. The line marked escapes the method body here points at the call that wants 'static data, and that call tells you which cause below applies.
cargo build 2>&1 | grep -n -A14 "E0521"Then find the line you see in this list and open its fix:
What does the error look like?
error[E0521]: borrowed data escapes outside of method`self` escapes the method body hereargument requires that `'1` must outlive `'static`
Cause 1 of 2
std::thread::spawn is called with a closure that uses self
You seeThe marked span is a std::thread::spawn call inside a method that takes &self or &mut self
Check
The signature of std::thread::spawn requires the closure to be Send and 'static, because the new thread may outlive the call that created it. A closure that reads self.field holds a borrow of self, which only lives as long as the method call.
grep -n "thread::spawn" src/*.rsFix
If the method can wait for the work, spawn inside std::thread::scope: scoped threads may borrow non-'static data because they are all joined before scope returns. If the thread must keep running, clone what it needs and move the clones in.
impl Worker {
fn run(&self) {
std::thread::scope(|s| {
s.spawn(|| {
println!("working on {}", self.name);
});
});
}
fn run_detached(&self) {
let name = self.name.clone();
std::thread::spawn(move || println!("working on {name}"));
}
}Verify
cargo build finishes without E0521, and the scoped version prints before run returns.
cargo build && cargo testConfirmed by doc.rust-lang.org
Cause 2 of 2
tokio::spawn is given an async block that borrows self
You seeThe marked span is tokio::spawn inside an async method on &self
Check
tokio::spawn takes a future that is Send and 'static. Tokio's tutorial explains that a spawned task must not hold references to data owned outside it, so an async block that reads self.client still borrows self and cannot be spawned.
grep -n "tokio::spawn" src/*.rsFix
Give the task its own data. Keep shared state in an Arc, clone the Arc in the method, and move the clone into an async move block.
use std::sync::Arc;
struct Service {
client: Arc<Client>,
}
impl Service {
fn start(&self) {
let client = Arc::clone(&self.client);
tokio::spawn(async move {
client.ping().await;
});
}
}Verify
The crate compiles and the task runs after start returns.
cargo buildConfirmed by tokio.rs
How do I stop it coming back?
- Treat any API whose bound includes 'static as a sign that the data must be owned, cloned into an Arc, or scoped before you call it.
- Keep fields that background work shares behind Arc from the start, so spawning a task only needs a cheap clone.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.