How to multiply two vectors in Rust

1 Answer

0 votes
fn multiply_vectors(vec1: &[i32], vec2: &[i32]) -> Vec<i32> {
    vec1.iter()
        .zip(vec2.iter())
        .map(|(a, b)| a * b)
        .collect()
}

fn main() {
    let vec1 = vec![1, 2, 3, 4];
    let vec2 = vec![5, 6, 7, 8];

    let result = multiply_vectors(&vec1, &vec2);

    for val in result {
        print!("{} ", val);
    }
}




/*
run:

5 12 21 32 

*/

 



answered Nov 3, 2025 by avibootz
...