This iterator
let data = vec![0, 1, 2, 3, 4, 5];
for x in data.chunks(2) {
println!("{:?}", x);
}
will produce
[0, 1]
[2, 3]
[4, 5]
Can I use iterator to get something like this.
[0, 1]
[1, 2]
[2, 3]
[3, 4]
[4, 5]
I know how to do that using for loop. But can iterator do this better?
I guess your can use Itertools.tuple_windows for this. According to the documentation it "returns an iterator over all contiguous windows producing tuples of a specific size (up to 4)" :
use itertools::Itertools;
use itertools::TupleWindows;
use std::slice::Iter;
let data = vec![0, 1, 2, 3, 4, 5];
let it: TupleWindows<Iter<'_, i32>, (&i32, &i32)> = data.iter().tuple_windows();
for elem in it {
println!("{:?}", elem);
}
Output:
(0, 1)
(1, 2)
(2, 3)
(3, 4)
(4, 5)
Edit:
As noted in comment1 by @Masklinn and comment2 by @SebastianRedl, you can also use windows
from stdlib and avoid including Itertools
in your project. But note that it only works for slices (or things that coerce to slices), not general iterators (which is fine in your case).
let data = vec![0, 1, 2, 3, 4, 5];
let it = data.windows(2);
for elem in it {
println!("{:?}", elem);
}
Output:
[0, 1]
[1, 2]
[2, 3]
[3, 4]
[4, 5]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With