E0521: borrowed data escapes outside of function Rust Origin: Application & API OSI layer 7 · Application Last updated 5 Nov 2026

Rust: error[E0521]: borrowed data escapes outside of function — `data` escapes the function body here (tokio::spawn requires 'static)

A reference was passed to something requiring 'static (spawn tasks/threads) but it borrows local data.

Exact message you may see error[E0521]: borrowed data escapes outside of function`data` escapes the function body hereargument requires that `'1` must outlive `'static`error[E0521]: borrowed data escapes outside of methodborrowed data escapes outside of function E0521
Seen on: Rust

Meaning

Move owned data into the task: clone (let data = data.clone();), wrap in Arc, or use String/Vec instead of &str/&[T]. For threads with borrowed data use std::thread::scope. Make function params T: 'static only if needed.

Common causes

  • tokio::spawn with a reference parameter
  • Closure capturing &self in spawn
  • &str argument used in a task
  • Trait objects without 'static
  • Callback stored beyond the function

⚡ Quick fix

  1. Convert to owned data (.to_owned(), Arc<T>) before spawning
  2. Use std::thread::scope for borrowed threading
  3. Add T: 'static bound if the type can be 'static

Detailed fix by platform

Rust

  1. let name = name.to_owned(); tokio::spawn(async move { println!("{name}"); });

How to diagnose

  1. Task — 'static?
  2. Data — Borrowed?
  3. Fix — Arc/clone/scope?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

Cannot start a runtime from within a runtime (Tokio) — Rust: thread 'main' panicked: Cannot start a runtime from within a runtime — a function like `block_on` tried to block a thread that drives async tasks (Rust)Cannot start a runtim…E0106: missing lifetime specifier — Rust: error[E0106]: missing lifetime specifier — this function's return type contains a borrowed value, but the signature does not say which one of `x`'s 2 lifetimes it is borrowed from (Rust)E0106: missing lifeti…E0373: closure may outlive the current function — Rust: error[E0373]: closure may outlive the current function, but it borrows `v`, which is owned by the current function (Rust)E0373: closure may ou…E0502 — Rust: error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immutable (Rust)E0502E0597: borrowed value does not live long enough — Rust: error[E0597]: `x` does not live long enough — borrowed value does not live long enough (Rust)E0597: borrowed value…E0716: temporary value dropped while borrowed — Rust: error[E0716]: temporary value dropped while borrowed — creates a temporary value which is freed while still in use (Rust)E0716: temporary valu…E0728: await is only allowed inside async functions and blocks — Rust: error[E0728]: `await` is only allowed inside `async` functions and blocks (Rust)E0728: await is only …E0752: main function is not allowed to be async — Rust: error[E0752]: `main` function is not allowed to be `async` (Rust)E0752: main function …E0521: bo…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.