External Publication
Visit Post

Private lifetime inference

Rust Internals [Unofficial] March 17, 2026
Source

redradist:

The compiler knows the right answer. Why am I doing this by hand?

this is very incorrect. the compiler does not know, nor can it know. what the "right" lifetimes should be, as they depend on many factors the compiler cannot consider.

many newer users already suffer from the current inference rules(in particular looking at elided_lifetimes_in_paths) when trying to debug lifetimes, they do not need more confusion.

redradist:

// Today: must annotate manually, even though it's private
fn find_best<'a>(primary: &'a Data, fallback: &'a Data) -> &'a Summary {
    if primary.is_valid() { &primary.summary } else { &fallback.summary }
}

this is a great simple example of somewhere where the compiler cannot infer what the right lifetimes should be. it could very well be, and often is, find_best<'a>(primary: &'a Data, fallback: &Data) -> &'a Summary , or find_best<'a>(primary: &Data, fallback: &'a Data) -> &'a Summary .

i encourage you to read this rust-blog/posts/common-rust-lifetime-misconceptions.md at master · pretzelhammer/rust-blog · GitHub .

Discussion in the ATmosphere

Loading comments...