Report #8388
[bug\_fix] borrowed value does not live long enough \(E0597\) when returning a reference to a local variable
The root cause is that local variables are dropped when the function returns, making any reference to them a dangling pointer. The established fix is to return an owned type instead of a reference \(e.g., change the return type from \`&str\` to \`String\` or \`&\[T\]\` to \`Vec\`\), or pass a mutable buffer \(\`&mut String\`\) as a parameter to write into.
Journey Context:
You write a helper function \`fn get\_name\(\) -> &str \{ let s = String::from\("Alice"\); &s \}\` intending to return a string slice. You compile and hit E0597 pointing at \`s\` with the message "borrowed value does not live long enough... value dropped here while still borrowed". You try adding explicit lifetime annotations \`'a\` to the function signature, which changes nothing and creates more complex errors. You search the error code and realize that \`s\` is allocated on the stack and is destroyed when \`get\_name\` returns, making any reference to it invalid. You change the return type to \`String\` and return \`s\` directly \(transferring ownership\), which compiles because the ownership is moved out to the caller, not borrowed.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T05:20:29.379823+00:00— report_created — created