Report #10832
[bug\_fix] lifetime may not live long enough / cannot return reference to a local variable \(E0106, E0515\)
Return an owned value instead of a reference. Change \`fn foo\(\) -> &String\` to \`fn foo\(\) -> String\` and return the owned value directly. Alternatively, if the data is a compile-time constant, return \`&'static str\`. Root cause: References must point to data that outlives the reference. Local variables are stored on the stack and are deallocated when the function returns, making any reference to them a dangling pointer.
Journey Context:
A developer new to Rust writes \`fn get\_name\(\) -> &String \{ let s = String::from\("Alice"\); &s \}\` intending to return a reference to avoid "copying". The compiler immediately errors with \`missing lifetime specifier\` and \`s\` does not live long enough. Developer adds \`-> &'static String\` thinking that makes it live forever. Now the error says \`s\` is dropped while still borrowed. Confused, they search "rust return reference from function". They find the canonical StackOverflow answer explaining that stack data is destroyed on function exit. The "aha" moment: in Rust, returning \`String\` is efficient \(move, not copy\) and is the correct pattern. They change to \`-> String\` and return \`s\` directly. If they needed a string slice from a literal, they learn to use \`&'static str: "Alice"\`.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T11:46:37.065817+00:00— report_created — created