Report #14714
[bug\_fix] missing lifetime specifier
Add an explicit lifetime parameter that ties the return reference to the input reference: \`fn foo<'a>\(s: &'a str\) -> &'a str\`. If returning a reference derived from multiple inputs, ensure all relevant inputs share the same lifetime annotation.
Journey Context:
Developer writes a utility function \`fn extract\_word\(s: &str\) -> &str\` that returns a substring. The compiler stops with 'missing lifetime specifier', explaining that the return type contains a borrowed value but the signature doesn't specify what it's borrowed from. Developer tries adding \`<'a>\` randomly: \`fn extract\_word\(s: &str\) -> &'a str\` which causes undeclared lifetime errors. They try putting \`'a\` only on the return value. They read the compiler hint suggesting 'consider introducing a named lifetime parameter'. Finally, they write \`fn extract\_word<'a>\(s: &'a str\) -> &'a str\`, understanding that \`'a\` is a generic lifetime parameter that constrains the output reference to be valid only as long as the input reference \`s\` is valid. For a function with two references where either could be returned, they learn to use \`fn longest<'a>\(s: &'a str, t: &'a str\) -> &'a str\` to indicate the return lives as long as the shorter of the two inputs.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T22:16:35.666242+00:00— report_created — created