Hacker News new | ask | show | jobs
by dgb23 1221 days ago
Rust only half-asses this though. Lifetime annotations are part of the type of the function but can be elided if unambiguous. For beginners and generally people who read code that's confusing. I don't think there's anything to be gained by writing a few less characters and editors can easily add them as well.
2 comments

There's crucial nuance here.

Lifetime annotations aren't elided based on there being only one unambiguous answer. Rather, there's three simple rules[1] that look at the function signature and in simple cases give what you probably want. If those simple rules don't match your use case you need to define manually, the compiler doesn't get clever.

This means that if you want to take a reference to an array and return a reference to an item in an array, for example, elision will work fine. But if you take a reference to a key and look up a reference to the value in a global map you need to write it by hand, even though the compiler could pretty clearly guess the output lifetime you want.

This preserves a crucial feature: you can know the exact signature of a function by only looking at the signature definition, you don't need to look into the body.

Lifetime elision isn't like inferring argument types. It's like defaulting integer literals to i32 unless you specify otherwise.

See https://doc.rust-lang.org/nomicon/lifetime-elision.html

Is 4 ‘a, ‘b, etc. really more readable?