[untitled]
32 pointsby jblazevic0 comments
fn multiply_safe(a : Vec<f64>, b : Vec<f64>) -> Vec<f64> {
if a.len() != b.len() {
panic!("The two vectors differ in length!");
}
return a.iter().zip(b.iter()).map(|(x, y)| x * y).collect()
}
Safer because you don't manipulate indices and lengths directly like you would in C, so there is no opportunity of an off-by-one bug or things like that. Surprisingly, rustc optimizer makes sure that this performs as well as the index-wrangling implementation (according to some simple tests I just ran).