Report #15514
[bug\_fix] error\[E0515\]: cannot return value referencing local variable \`s\`
Change the return type from \`&str\` to \`String\` to transfer ownership of the data out of the function. Alternatively, accept a \`&str\` parameter to borrow from the caller. The root cause is that local variables are dropped when the function scope ends, making any reference to them a dangling pointer.
Journey Context:
You're refactoring a helper function to return a \`&str\` instead of \`String\` to avoid allocations. You write \`fn get\_id\(\) -> &str \{ let s = String::from\("temp"\); &s \}\`. The compiler throws E0515. You try adding \`'static\` lifetime, then generic \`'a\` lifetimes, but the error persists. Searching reveals the fundamental rule: you cannot return references to data owned by the function itself because that data is deallocated when the stack frame is popped. The 'aha' moment is realizing ownership must be transferred \(return \`String\`\) or borrowed from the caller \(pass \`&str\` in\). You change the return type to \`String\` and the code compiles.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T00:20:16.580449+00:00— report_created — created