Report #80075
[bug\_fix] cannot return value referencing temporary value
Return an owned value \(e.g., \`String\`\) instead of a reference \(\`&str\`\), because the data is created within the function and cannot outlive the function's stack frame.
Journey Context:
Developer writes a function \`fn get\_name\(\) -> &str\` that constructs a \`String\` using \`format\!\("User \{\}", id\)\` and tries to return a slice \`&result\` or \`result.as\_str\(\)\`. The compiler errors that the return value references a temporary value owned by the current function. Developer tries to add explicit lifetime annotations \`'a\` on the return type and parameter, but there are no input parameters to tie the lifetime to. They try returning \`&'static str\` and leaking the memory with \`Box::leak\`, which works but feels wrong. They consider using \`Rc\` or \`Arc\`, but the simplest realization is that since the function creates the string, it should give ownership to the caller. Changing the return type to \`String\` and returning \`result\` directly solves the issue without unsafe code or complex lifetimes.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T17:00:41.920004+00:00— report_created — created