3 ms·
> fn greet(req: HttpRequest) -> impl Responder Nice to see front page example using 'impl', the recent most improvement in ergonomics. Before 1.26 things would
by sudeepj 8y ago
> fn greet(req: HttpRequest) -> impl Responder
Nice to see front page example using 'impl', the recent most improvement in ergonomics. Before 1.26 things would be different. This makes me more appreciative of the efforts from Rust team/community to improve the ease of use.
- rkangel 8y agoTo explain this to people who don't know the background - this is about 'impl NameOfTrait' in the return position. It allows functions to return an object that provides a certain interface without specifying the actual type of that object. This was only previously true by wrapping it in a 'box', which meant a heap allocation and dynamic dispatch. The 'impl trait' provides static dispatch and no other overhead, so produces equivalent code to returning the type directly, but with all the abstraction flexibility that you want.
- ejanus 8y agoWhat is the use case ? Is there simple examples one could go through?
- iopq 8y agoUsually you return a closure with impl Fn. Another use case is to return an iterator. I wrote a post about it. Let me know if it helps. https://medium.com/@iopguy/impl-trait-in-rust-explanation-efde0d94946a https://medium.com/@iopguy/impl-trait-in-rust-explanation-ef...
- steveklabnik 8y agohttps://blog.rust-lang.org/2018/05/10/Rust-1.26.html https://blog.rust-lang.org/2018/05/10/Rust-1.26.html has a section about it.
- ejanus 8y agoThanks