Report #93226
[bug\_fix] lifetime may not live long enough: returning value referencing data owned by current function \(E0515\)
Return an owned type \(e.g., \`String\` instead of \`&str\`\) or change the function to accept a reference with an explicit lifetime parameter \`'a\` and return \`&'a T\`, ensuring the returned reference does not outlive the input data. Root cause: References in return values cannot point to data allocated on the function's stack \(local variables\) because that stack frame is deallocated when the function returns, creating a dangling pointer.
Journey Context:
You write a helper function that constructs a \`String\` using \`format\!\` and try to return \`&str\` to avoid allocation in the caller. The compiler throws E0515: 'cannot return reference to temporary value'. You first try to add \`'static\` to the return type, which creates more errors. You search the error code and land on the Rustonomicon chapter on lifetimes. You realize that data created inside the function \(the String\) is owned by the function's scope and will be dropped at the end, so any reference to it would dangle. You have two options: return the \`String\` itself \(transferring ownership\), or accept a \`&'a str\` as input and return \`&'a str\` as output. You choose to return \`String\` because the helper needs to construct the data anyway. After changing the return type and updating the caller to handle the owned String, the code compiles and is memory-safe.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T15:03:59.962474+00:00— report_created — created