4 ms·
Part of Rust's ownership system are 'lifetimes.' Without getting into the details, they're an annotation you occasionally add to code in certain situations. Lik
by steveklabnik 12y ago
Part of Rust's ownership system are 'lifetimes.' Without getting into the details, they're an annotation you occasionally add to code in certain situations. Like these two:
fn get_mut<'a>(&'a mut self) -> &'a mut T;
fn print<'a>(s: &'a str);
The 'a parts are the lifetime annotations.
We used to have a single inference rule for when you didn't need to write the annotation. With RFC 39[1], we added two more. These two extra inference rules removed 87% of the existing annotations, giving you this instead:
fn get_mut(&mut self) -> &mut T;
fn print(s: &str);
This is a pretty huge readability win.
1: https://github.com/rust-lang/rfcs/blob/master/active/0039-lifetime-elision.md https://github.com/rust-lang/rfcs/blob/master/active/0039-li...
- untothebreach 12y agooh ok, thanks. i knew what lifetimes were, but was confused by the 'elision.' Apparently I didn't connect 'elision' with 'elide.' So, new vocab word for today :)
- haberman 12y agoWhat kind of cases do you still need to write explicit lifetimes?
- steveklabnik 12y agoI actually haven't had to since elision was implemented, so I can't give you a good rule of thumb from experience. Here's an overview of how the rules applied to the standard library at the time they were implemented, which is where the percentage came from: https://gist.github.com/aturon/da49a6d00099fdb0e861 https://gist.github.com/aturon/da49a6d00099fdb0e861
- xKingfisher 12y agoThe most common case I have run into is storing a reference inside another data structure.