Rust: error[E0597]: `x` does not live long enough — borrowed value does not live long enough
A reference outlives the value it points to (the value is dropped first).
error[E0597]: `x` does not live long enoughborrowed value does not live long enoughE0597 `x` dropped here while still borroweddoes not live long enough borrowed value`x` does not live long enough binding `x` declared here Meaning
Typical: storing &local in something that lives longer, returning a reference to a local, or inner-scope values referenced from outer variables. Fix by moving the owner to a longer scope, returning owned data, cloning, or using Rc/Arc. In threads use thread::scope or move ownership.
Common causes
- Reference to a local stored in an outer variable
- Returning
&local - Struct holding a reference to a shorter-lived value
- Closure capturing a reference past its scope
- Temporary dropped at end of statement
⚡ Quick fix
- Declare the owner in the outer scope
- Return/store owned values (
to_string(),clone()) - Use
std::thread::scopefor borrowing across threads
Detailed fix by platform
Rust
let owner = String::from("x"); let r = &owner; // owner declared before r's use scope
How to diagnose
- Owner — Dropped when?
- Reference — Used after?
- Fix — Scope or ownership?
🔧 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
- Blocking waiting for file lock on package cache / build directory Cargo: Blocking waiting for file lock on package cache / on build directory
- 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
- E0308 Rust: error[E0308]: mismatched types — expected `X`, found `Y`
- E0382 Rust: error[E0382]: borrow of moved value: `x`
- feature edition2024 is required / Cargo.lock version too new Cargo: error: failed to parse manifest — feature `edition2024` is required / lock file version 4 requires `-Znext-lockfile-bump`
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