I was chat with a friend who is learning Rust, and he asked me why can't the compiler "guess" the lifetimes, and why do they need to be explicit. He comes from C++ where templates have exactly that properties (ie they are analyzed post-monomorphisation, and not pre-monomorphisation like Rust generics). This means that a C++ compiler would look at the body of the function and extract the lifetime information. In contrast a Rust compiler will look at the body of the function and validate that the lifetime information match what was declared in the prototype. Having pre-monomorphisation diagnostics have a very nice impact both in term of error reporting, and compilation speed. Nonetheless his question is valid. Why can't the compiler extract the lifetime information. I'm sure that I read an article somewhere that was giving an example of a lifetime that could not be inferred correctly and would result in a code code harder/impossible to use (a variable was borrowed for more time than needed).
Do you have examples of code where the compiler couldn't "guess" correctly the lifetimes.