Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

Rust error E0521 when a method passes self to a thread or a Tokio task

Your error

error[E0521]: borrowed data escapes outside of method2 more lines it can show

Fix it fast

Most common cause, 1 of 2

std::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.

Fix
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}"));
    }
}
Did this fix it?
Full steps for this cause: check, fix, verify

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.

  • Applies to Rust 1.100 (stable, Nov 2026), Tokio 1.54.1
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
cargo build 2>&1 | grep -n -A14 "E0521"

Then find the line you see in this list and open its fix:

  1. 1std::thread::spawn is called with a closure that uses selfThe marked span is a std::thread::spawn call inside a method that takes &self or &mut self
  2. 2tokio::spawn is given an async block that borrows selfThe marked span is tokio::spawn inside an async method on &self

What does the error look like?

  • error[E0521]: borrowed data escapes outside of method
  • `self` escapes the method body here
  • argument 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.

Check
grep -n "thread::spawn" src/*.rs

Fix

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.

Fix
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.

Verify
cargo build && cargo test

Confirmed by doc.rust-lang.org

Did this fix it?

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.

Check
grep -n "tokio::spawn" src/*.rs

Fix

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.

Fix
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.

Verify
cargo build

Confirmed by tokio.rs

Did this fix it?

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.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.