Report #17898
[bug\_fix] cannot return value referencing local variable \`s\` \(E0515\)
Change the return type from a reference \(\`&str\`\) to an owned type \(\`String\`\) and return the owned value directly, removing the reference.
Journey Context:
Developer writes a helper function \`fn format\_data\(input: &str\) -> &str\`. Inside, they create a \`String\` via \`let s = format\!\("prefix-\{\}", input\);\` and try to return \`&s\[..\]\` or \`s.as\_str\(\)\`. The compiler errors that they cannot return a reference to \`s\` because \`s\` is owned by the function and will be dropped at the end of the scope, creating a dangling pointer. Developer tries to add a lifetime \`'a\` to the return type but realizes the data doesn't live long enough. They search and learn that functions cannot return references to data created inside themselves \(unless leaking with \`Box::leak\`, which is usually wrong\). The solution is to return the \`String\` itself.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T06:44:46.979388+00:00— report_created — created