As I understand it, internal iterators are iterators that, in addition to the logic of the closure itself, also perform logic. And the adapters wrap the logic in the closure, are they internal?
So what's the internal iterator from your point of view?
Oh, I'm confused
I corrected the post
Internal vs external iterator is not about whether something "performs additional logic". That's simply not a useful distinction — all iterator adaptors serve some purpose, so they all do something.
Internal vs external iteration refers to how iteration is exposed as an API:
- external iteration creates an explicit iterator state that you as the user of the iterator can control (ie., decide when to advance it). This is the predominant pattern in Rust: you can explicitly advance an iterator by calling its
next()method, and in a for loop, you can intersperse arbitrary code with using the iterator's items.
- internal iteration means that the act of iteration is achieved via callbacks, and when/how exactly those callbacks are called is not controlled directly by the consumer of the iterator, but by the iterator itself. This is approximately what the
for_each()iterator method does.
The Rust stdlib iteration model is external as the caller can choose when to advance the iterator, or even not advance it at all.
While the possibilities of what a caller can do using internal iteration is a strict subset of what they can do using the external model, it can be easier to implement internal iteration for certain data structures (e.g. using recursion). That's what the internal-iterator crate allows you to do.
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.