Most iterators are FusedIterator
; once they return None
, they will always keep doing so. But they don't have to be:
- An API produces data. Sometimes there is new data available, sometimes there isn't (but there could be later). This is modeled quite nicely by an un-fused iterator that returns
None
in the "no new data" case.- If you are using
async
, aStream
/AsyncIterator
may be a better choice, bit sometimes you don't want to rearchitect your entire application to beasync
.
- If you are using
- A cursor over a cyclic data structure (like
linked_list::Cursor
)—these can also be modeled as an un-fused iterator, and in fact theCursor
docs make the analogy. (One issue is the lack of a trait for reversing the direction of iteration without changing the position.)
I used an un-fused iterator just yesterday, I think they are neat and a useful tool to have in one's toolbox. However, when I brought up the question of supporting un-fused iteration in the upcoming gen
blocks feature, it became apparent that mine is a minority view:
- `yield` without value from a `gen` block · Issue #123614 · rust-lang/rust · GitHub
- Add `gen` blocks and reserve keyword in Rust 2024 by oli-obk · Pull Request #3513 · rust-lang/rfcs · GitHub
Have you used deliberately unfused iterators before? If so, what for? If not, why not, and what do you prefer instead?