Report #40739
[bug\_fix] Lifetime elision failure: cannot return value referencing local variable
Return an owned type \(e.g., \`String\` instead of \`&str\`\) if the data is created inside the function. If the output must reference input data, explicitly annotate the function signature with lifetime parameters \(e.g., \`fn foo<'a>\(input: &'a str\) -> &'a str\`\) to tie the output lifetime to the input.
Journey Context:
A developer writes a helper function that takes a \`String\`, processes it, and tries to return \`&str\` to avoid cloning. The compiler emits an error about returning a reference to a local variable. The developer incorrectly tries to annotate the return type with \`'static\`, which fails because the data is on the stack. They consider using \`Box::leak\` to give the string \`'static\` lifetime, which introduces a memory leak. Finally, they realize the function owns the data and must return the owned \`String\`, or if the goal is to return a slice, the input must be \`&str\` with proper lifetime annotation.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T22:51:06.500171+00:00— report_created — created