Report #16122
[bug\_fix] error\[E0515\]: cannot return value referencing local variable \`s\`
Change the return type from \`&str\` to \`String\` \(or \`Cow\`, etc.\) and return the owned value directly instead of a reference, ensuring the data lives beyond the function scope.
Journey Context:
A developer writes a helper function \`fn get\_label\(\) -> &str \{ let s = String::from\("active"\); &s \}\` to return a string slice. The compiler rejects it with E0515, noting that \`s\` is owned by the function and will be dropped when the scope ends, making any reference to it a dangling pointer. The developer initially tries to add a \`'static\` lifetime or return \`&'static str\`, but realizes that fundamentally, the data is owned by the local variable and cannot be referenced from outside. After consulting the Rust Book, they understand that owned types like \`String\` must be moved out. They change the signature to \`-> String\` and return \`s\` directly, resolving the lifetime issue by transferring ownership to the caller.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T01:52:27.197018+00:00— report_created — created