itertools
itertools copied to clipboard
Laziness in tuple_windows
I was bit surprised by that Itertools::tuple_windows is not somewhat lazy and has side effect.
It is consuming the first tuple when calling Itertools::tuple_windows .
https://doc.rust-lang.org/nightly/core/iter/index.html#laziness says that adapters in Iterator is lazy and
the TupleWindows has #[must_use = "iterator adaptors are lazy and do nothing unless consumed] since #227
So the question is
- Is this intended behavior?
- Should (all) adapters in
Itertoolsbe lazy? - Should the laziness be documented?
I think this is a bug to be fixed...
I looked for (mut text in function definitions and found multiple functions I thought as lazy to actually consume one item before we actually consume them:
- tuple_windows relying on the struct TupleWindows as mentioned before ;
- coalesce, dedup_by, dedup_by_with_count all three relying on the struct "CoalesceBy" ;
- cartesian_product relying on the struct "Product".
CoalesceBy and Product also uses #[must_use = "iterator adaptors are lazy and do nothing unless consumed"].
Now that I think about it, I suppose we could argue that "must_use" implies that the user is supposed to consume the iterator and consume one item at definition is not a big deal since it's "just a moment" before the rest?!