Rust: error[E0716]: temporary value dropped while borrowed — creates a temporary value which is freed while still in use
A reference points to a temporary that's dropped at the end of the statement.
error[E0716]: temporary value dropped while borrowedcreates a temporary value which is freed while still in usetemporary value is freed at the end of this statementconsider using a `let` binding to create a longer lived value Meaning
Bind the temporary to a variable so it lives longer: let s = String::from("x"); let t = s.trim();. Common with .to_string().as_str(), format!(..).as_str(), &String::new() in struct fields, or lock().unwrap().get(...) references.
Common causes
foo(&String::from("x"))result kept.to_string().as_str()storedformat!(...).trim()stored in a variable- Mutex guard temporary
- Iterator over a temporary collection
⚡ Quick fix
- Store the owner in a
letfirst - Return owned data
- Avoid chaining borrow after a temporary when keeping the result
Detailed fix by platform
Rust
let owned = format!("{a}-{b}"); let part = owned.trim();
How to diagnose
- Temporary — Which expression?
- Lifetime — Needed longer?
- Fix — let binding?
🔧 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
- cargo: failed to get from registry (spurious network error) Cargo: warning: spurious network error (2 tries remaining): [35] SSL connect error / error: failed to download from `https://index.crates.io/...`
- cargo: failed to select a version for the requirement Cargo: error: failed to select a version for the requirement `serde = "^2.0"` — candidate versions found which didn't match: 1.0.197
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