Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid loops when iterating over adjacent elements of a vector

In Rust, how do I avoid writing these loops? The code takes a vector and multiplies three adjacent elements to a product. Hence, the outer loop goes over all elements that can form a group of three and the inner loop does the multiplication.

The difficulty lies, I think, in the incomplete iteration of the outer loop (from element 0 to last - 3). Further, the inner loop must use a sub-range.

Is there a way to avoid writing the loops?

let v = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9];
let mut products = Vec::new();
for seq in 0..v.len() - 3 {
    let mut product = 1;
    for offset in 0..3 {
        product *= v[seq + offset];
    }
    products.push(product);
}
like image 659
dani Avatar asked Apr 21 '17 07:04

dani


1 Answers

The function you are searching for is [T]::windows(). You can specify the size of the overlapping windows and it will return an iterator over sub-slices.

You can obtain the product of all elements within a sub-slice by making an iterator out of it and using Iterator::product().

let v = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9];
let products: Vec<u64> = v.windows(3)
    .map(|win| win.iter().product())
    .collect();

(Playground)

Here we collect all products into a new vector.


A last note: instead of writing down all numbers in the vector manually you could write this instead:

let v: Vec<_> = (1..10).chain(1..10).collect();
like image 187
Lukas Kalbertodt Avatar answered Nov 12 '22 00:11

Lukas Kalbertodt