Report #12711
[bug\_fix] missing lifetime specifier or lifetime may not live long enough
Explicitly annotate the lifetime parameter to establish that the returned reference borrows from the input reference: change \`fn foo\(s: &str\) -> &str\` to \`fn foo<'a>\(s: &'a str\) -> &'a str\`.
Journey Context:
A developer writes a utility function \`extract\_host\` that takes a URL string slice and returns the host portion as a string slice. They write \`fn extract\_host\(url: &str\) -> &str\` and use string slicing to return \`&url\[start..end\]\`. The compiler stops with 'missing lifetime specifier' on the return type. The developer, coming from C\+\+, assumes the compiler should infer that the returned reference lives as long as the input. They try changing the return type to \`&'static str\`, which compiles for string literals but fails later with 'lifetime may not live long enough' when they try to use the function with non-static input. They then try adding \`'a\` randomly to both parameters and return types without understanding the syntax. After reading the compiler error explanation, they realize that in Rust, every reference has a lifetime, and the compiler cannot assume which input a returned reference borrows from without explicit annotation. The epiphany is that the lifetime parameter \`'a\` acts as a contract linking the input and output. The fix is to declare \`fn extract\_host<'a>\(url: &'a str\) -> &'a str\`, explicitly telling the compiler that the returned slice borrows from \`url\` and cannot outlive it.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T16:46:04.184548+00:00— report_created — created