I have been doing some reading and discovered that indexing in Rust is not the same as C++, in Rust there is additional bounds checking when doing my_vec[2] to ensure the index is within bounds. I am writing some performance critical code and have a loop that I currently use
let num_points_in_vector = new_vec.data.len() / 3;
for i in 0..new_vec.data.len()/3 {
new_vec.data[i] = other_vec[i * 3];
new_vec.data[num_points_in_vector + i] = other_vec[i * 3 + 1];
new_vec.data[num_points_in_vector * 2 + i] = other_vec[i * 3 + 2];
}
I have read that iterators about the most efficient way to loop through a vector. So how would I rewrite this loop to use iterators?
sorry for what is likely a dumb question, but I am quite new to Rust and it's concept of zipping. What would the izip! version look like? Thanks in advance!
is there any effective difference between for_each and for? Or is it basically syntactic sugar and it compiles down to the same thing? Thanks for the tips.
A for loop calls Iterator::next repeatedly, whereas for_each is basically a fold with an empty () accumulator. It only makes a difference if the iterator specializes fold to do something better than repeating next() in a loop.
For example, Chain::next has to check its state on every call to see if it's on the first or second part of the chain. But in Chain::fold it can just fold the first part entirely, then fold the second part entirely. Sometimes the compiler optimizer can figure this out in a for loop anyway, "hoisting" the condition out of the loop, but it's more reliable to do that explicitly.