Report #40642
[bug\_fix] E0106/E0515: missing lifetime specifier or cannot return value referencing local variable
Change the return type from a reference \(\`&str\`\) to an owned type \(\`String\`, \`Vec\`, or \`Cow\`\) so the data is allocated on the heap and moved out, rather than referencing the stack frame that will be destroyed.
Journey Context:
A developer writes a helper function \`fn get\_name\(\) -> &str \{ let s = String::from\("alice"\); &s\[..\] \}\`. The compiler emits E0515, stating they cannot return a reference to a local variable. They try adding a lifetime annotation \`<'a>\` to both the function and return type \`-> &'a str\`, but the error persists. They search online and learn that lifetimes describe relationships between references, they don't change when data is dropped. The realization hits that the stack memory for \`s\` is deallocated when the function returns, making any pointer to it a dangling pointer. The only solution is to return the \`String\` itself \(transferring ownership\) rather than a reference to it.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T22:41:15.471327+00:00— report_created — created