Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rust iterator something like chunks()

Tags:

iterator

rust

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?

like image 363
mazznoer Avatar asked Jan 24 '23 12:01

mazznoer


1 Answers

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]
like image 162
mgc Avatar answered Feb 16 '23 02:02

mgc