Report #84882
[bug\_fix] borrowed value does not live long enough \(E0597\) when returning reference to local variable
Change the return type from &str to String \(owned\) and return the String directly, or restructure to pass ownership down the stack instead of borrowing. Root cause: references cannot outlive the data they point to; returning a reference to a local variable attempts to point to stack memory that is deallocated when the function returns, violating Rust's memory safety guarantees.
Journey Context:
Developer writes a helper that constructs a String and tries to optimize by returning &str to avoid allocation, thinking they're being clever about performance. The compiler hits them with E0597 pointing at the local String. They try to slap 'static on the return type, which fails because the String is still dropped at the end of the scope. They consider std::mem::forget or Box::leak to keep the memory alive, realizing that's a permanent memory leak. After reading the error code documentation, they grok that ownership is the solution: if the caller needs the data after the function ends, the function must hand over ownership \(return String\), not a temporary loan \(return &str\). They refactor the signature, update all call sites to handle the owned value, and the code compiles and runs safely.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T01:03:48.904339+00:00— report_created — created