I have a function called new_vec
. It takes two vectors and creates a new one, by performing an elementwise operation on the pair of elements from the zipped vectors.
fn main() {
let v1s = vec![1, 0, 1];
let v2s = vec![0, 1, 1];
let v3s = new_vec(v1s, v2s);
println!("{:?}", v3s) // [1, 1, 2]
}
fn new_vec(v1s: Vec<i32>, v2s: Vec<i32>) -> Vec<i32> {
let mut v3s = Vec::<i32>::new();
for (v1, v2) in v1s.iter().zip(v2s.iter()) {
v3s.push(v1 + v2) // would also like to use -
}
v3s
}
I want to have a new_vec
function for the common binary operation that is possible to use on two integers, such as +
, -
, /
, *
.
How do I do this? I can imagine two ways: macros and closures. A minimal example of how to do this in the best way, for example with +
and -
would be appreciated.