Report #91223
[bug\_fix] cannot infer an appropriate lifetime for borrow expression due to conflicting requirements \[E0495\]
Explicitly annotate lifetimes on the function signature \(e.g., \`fn foo<'a>\(x: &'a str\) -> &'a str\`\), ensure the returned reference does not outlive the input, or restructure to return an owned value \(\`String\` instead of \`&str\`\) to decouple lifetimes. Root cause: The borrow checker cannot prove that the returned reference lives at least as long as the input reference or the caller requires; explicit lifetime annotations establish this contract.
Journey Context:
Developer writes a function \`extract\_domain\(url: &str\) -> &str\` that parses a URL and returns a slice of the input. They omit lifetime annotations, expecting elision to work. However, the body uses intermediate variables or complex logic, and the compiler emits E0495, noting it cannot infer the lifetime relationship. Developer tries adding \`'a\` to the return type only, but the compiler complains about the input. They realize they need to declare a lifetime parameter \`'a\` and tie both input and output to it: \`fn extract\_domain<'a>\(url: &'a str\) -> &'a str\`. They also check that they are not returning a reference to a local temporary string created inside the function. The fix works because it explicitly promises the compiler that the returned slice borrows from \`url\` and will not outlive it, satisfying the borrow checker's region constraints.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T11:42:36.959445+00:00— report_created — created