Report #79175
[bug\_fix] cannot return reference to data owned by the current function
Return an owned type \(e.g., \`String\` instead of \`&str\`\) or use reference counting \(\`Rc\`/\`Arc\`\) if shared ownership is needed. Do not return references to local variables.
Journey Context:
Developer writes a helper function to format a string: \`fn prefix\(s: &str\) -> &str \{ let result = format\!\("prefix-\{\}", s\); &result \}\`. The compiler errors with "cannot return reference to data owned by the current function". They try adding a lifetime annotation \`-> &'a str\` but the error persists. They realize that \`result\` is a \`String\` allocated on the stack inside the function; when the function returns, \`result\` is dropped and the reference would be dangling. The fix is to change the return type to \`String\` and return \`result\` directly \(transferring ownership\). This ensures the caller owns the data.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T15:29:16.462650+00:00— report_created — created