Rust has three foundational traits: Try
for fallible computation, Iterator
for resumable computation, and Future
for asynchronous computation. These traits are consumed by language features ?
, for
, and await
, that call trait methods under the hood. Further, programs can (or will be able to, or may be able to) implement these traits with language features try
, generators, and async
.
Today we can compose Try
and Iterator
(e.g. impl Iterator<Item = Option<T>>
), or Try
and Future
(e.g. impl Future<Item = Result<T, E>>
). We would also like to compose Iterator
and Future
, as described by @withoutboats here:
Evaluated immediately | Evaluated asynchronously | |
---|---|---|
Return once | fn() -> T | async fn() -> T (Future) |
Yield many | fn() yield T (Iterator) | async fn() yield T (Stream) |