Hot on the heels of last week’s call to action about the module system changes in Rust 2018, this week we’d like to draw attention to and make a call for action on '_
(wildcard) lifetimes coming to Rust 2018!
One of the features of RFC 2115 is the “wildcard lifetime”, written as '_
. Today lifetime elision allows signatures like:
fn foo(&self) -> Ref<SomeType>
fn iter(&self) -> Iter<T>
but these are hiding the fact that there’s a connected lifetime there. To ergonomically convey that there’s a lifetime contained inside you can now write this as:
fn foo(&self) -> Ref<'_, SomeType>
fn iter(&self) -> Iter<'_, T>
and the two snippets mean the same thing!
It’s not always easy to find locations in your crate that can use from '_
, we’re after all trying to fix the problem that they’re so hidden! To help you with the transition we’ve developed a new lint for this feature, and the easiest way to follow the lint is to use cargo fix
. Note that you do not need to be in the 2018 edition to test this feature.
- Add
#![feature(rust_2018_preview)]
to your crate (skip this if you’re already on the 2018 edition) - Add
#![warn(elided_lifetimes_in_paths)]
to your crate - Run
cargo fix
And that’s it! If you see any bugs please report them to rust-lang/cargo or rust-lang/rust. This includes things like cases the compiler should have linted for but didn’t, lint suggestions that fail to apply, etc.
Afterwards be sure to run cargo doc
, try it out with the RLS, run rustfmt
to see what happens, otherwise try to stress this feature! Like the module system we want to make sure that wildcard lifetimes are as polished as they can be for the 2018 edition, and we need your help!
In addition to testing our your own code and reporting bugs, we’d love to see some community-driven tutorials/documentation/podcasts about the feature! If you’re busy this week don’t worry as well, we’ll be making a call to action for a new feature next week!
And of course, please feel free to leave feedback here as well!