Skip to content

Instantly share code, notes, and snippets.

@timClicks
Last active June 21, 2020 09:01
Show Gist options
  • Save timClicks/809fa86c5ae1aa26ec9bb42a4a4df401 to your computer and use it in GitHub Desktop.
Save timClicks/809fa86c5ae1aa26ec9bb42a4a4df401 to your computer and use it in GitHub Desktop.
How to .zip() through items in two arrays in Rust
fn main() {
let x_coordinates: [f32; 4] = [0., 1., 2., 3.];
let y_coordinates: [f32; 4] = [0., 1., 4., 9.];
for (x, y) in x_coordinates.iter().zip(y_coordinates.iter()) {
// Comparisons require that you dereference the values.
// iter() for arrays returns a reference to each element.
if *x <= 0.0 || *y <= 1.0 {
continue;
}
// Most of the time though, Rust's auto-dereferencing rules
// help us out to remove some of the noise.
println!("x = {}; ln(y) = {}", x, y.ln());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment