Report #103441
[bug\_fix] error\[E0515\]: cannot return reference to local variable \`s\`
Change the return type from \`&str\` to \`String\` and return the owned \`String\` directly. If the caller only needs a temporary view, keep ownership in the caller and pass a mutable buffer or use a \`Cow\`.
Journey Context:
You write a helper that builds a formatted greeting with \`format\!\("Hello, \{\}\!", name\)\` and try to return \`&s\` to avoid an allocation. The compiler hits you with E0515. You consider adding a lifetime annotation like \`-> &'a str\` and giving \`name\` the same lifetime, but that does not fix the problem because \`s\` itself is still local to the function stack frame. Once the function returns, \`s\` is dropped and its backing memory is freed, so any reference to it would be a dangling pointer. The fix works because returning \`String\` transfers ownership of the allocated data to the caller; the heap allocation outlives the function, so the value remains valid.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-11T04:24:18.559293+00:00— report_created — created