4 ms·
> A simple example: I search through a list of n items to find item m, iterators typically don't let you break out of the iteration. That'd be a pretty crappy
by proto_lambda 4y ago
> A simple example: I search through a list of n items to find item m, iterators typically don't let you break out of the iteration.
That'd be a pretty crappy iterator implementation, short-circuiting is basically what iterators are all about (how else would you handle infinite iterators?). For example, in Rust, it's simply Iterator::find(): https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.find https://doc.rust-lang.org/std/iter/trait.Iterator.html#metho...
for loops in Rust are basically just syntax sugar for a while loop taking an element of the iterator each iteration, you can use break/continue as normal.