rust-lang / futures-rs

Zero-cost asynchronous programming in Rust
https://rust-lang.github.io/futures-rs/
Apache License 2.0
5.29k stars 607 forks source link

Analogue of .last() method #2822

Open xamgore opened 6 months ago

xamgore commented 6 months ago

I'd like to run FuturesOrdered::from_iter(...) and retrieve the result of the last element, which is Option<...>. The same function exists in std.

It can be implemented with

iter.fold(None, |_acc, x| async move { Some(x) })

But try_last would require extra checks: if there is an intermediate error, return it. If not, return the last successful Ok(...).