Google News
logo
Rust - Interview Questions
What is the difference between an iterator and a generator?
In Rust, an iterator is a trait that defines a sequence of elements that can be iterated over using a for loop or other iteration constructs.

An iterator produces a sequence of values on demand and can iterate over any collection that implements the Iterator trait.

On the other hand, a generator is a type of iterator that produces values lazily and on-demand instead of eagerly generating all values upfront. Generators are defined using the yield keyword and can be used to represent infinite or very large sequences.
Advertisement