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.
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 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::spawnwith a reference parameter- Closure capturing
&selfin spawn &strargument used in a task- Trait objects without
'static - Callback stored beyond the function
⚡ Quick fix
- Convert to owned data (
.to_owned(),Arc<T>) before spawning - Use
std::thread::scopefor borrowed threading - Add
T: 'staticbound if the type can be 'static
Detailed fix by platform
Rust
let name = name.to_owned(); tokio::spawn(async move { println!("{name}"); });
How to diagnose
- Task — 'static?
- Data — Borrowed?
- 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:
Similar errors
- 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
- 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
- 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
Most viewed in Rust
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 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.
Report a correction or suggest an improvement
Last updated 5 Nov 2026