by losvedir · 10y ago · 0 comments
fn get_row(id: i32) -> impl Future<Item = Row>;
That return type looks odd to me. What does it mean to return an "impl", and is that a new feature in rust, or just something advanced that I missed in my exploration before?- http://aturon.github.io/blog/2015/09/28/impl-trait/ - https://github.com/rust-lang/rfcs/pull/1522
This feature allows you to return any struct that implements the trait, without having to type the name of the struct (which can sometimes be quite big). It also means that clients only know what traits are implemented; the concrete type is invisible to them. The above links have a bunch more detail.
(And this feature is set to land in nightly Rust very soon! Shoutout to eddyb :)
There may or may not be some extensions (like storing them in fields of structs) in the future.
Note that Rust supported existentials before too, in the form of trait objects. But this was only possible behind a pointer and using virtual dispatch, so the performance story wasn't perfect. The impl Trait syntax is supported through monomorphization.
fn did_too_many_chains() -> Chain<Map<'a, (int, u8), u16, Enumerate<Filter<'a, u8, vec::MoveItems<u8>>>>, SkipWhile<'a, u16, Map<'a, &u16, u16, slice::Items<u16>>>>
Now, you can write: fn did_too_many_chains() -> impl Iterator
and be done with it.https://github.com/rust-lang/rfcs/blob/master/text/1522-cons... is an explanation of the feature in detail, but it boils down to "I will return you some kind of thing that implements the Future trait, but I'm not telling you exactly what it is."
All of Rust's standard collection and smaht pointer types are written in pure Rust and have relatively complex variance behaviour.
https://github.com/rust-lang/rfcs/blob/master/text/1522-cons...
I notice it's consistent with the standard `Iterator<Item = ...>`, but why is Iterator like that?