Report #97183
[bug\_fix] error\[E0106\]: missing lifetime specifier
Add an explicit lifetime parameter that ties the returned reference to the input references, e.g. \`fn longest<'a>\(x: &'a str, y: &'a str\) -> &'a str\`. If the function returns one of several references, the output lifetime must be a lifetime the caller can rely on, which means it must be derived from the inputs.
Journey Context:
You write a helper \`fn longest\(x: &str, y: &str\) -> &str \{ if x.len\(\) > y.len\(\) \{ x \} else \{ y \} \}\` and expect it to work like the \`first\_word\` example from the book. Instead the compiler errors with E0106. You try adding \`'\_\` and learn it only works when there is exactly one input reference. With two input references, Rust cannot know whether the returned slice lives as long as \`x\` or as long as \`y\`. After reading the lifetime syntax chapter, you add a single generic lifetime \`'a\` to both inputs and the output, telling the compiler the returned reference is valid at least as long as both inputs are. The function now compiles and callers can use the result safely within that shared lifetime.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-25T04:41:30.987257+00:00— report_created — created