rust_linear_algebra/vector/ops/
sub.rs

1use super::super::Vector;
2use std::ops::Sub;
3
4impl<K> Sub for Vector<K>
5where
6    K: Sub<Output = K> + Copy,
7{
8    type Output = Vector<K>;
9
10    fn sub(self, rhs: Self) -> Self::Output {
11        let elements = self.iter().zip(rhs.iter()).map(|(&a, &b)| a - b).collect();
12        Vector { elements }
13    }
14}
15
16impl<K> Vector<K>
17where
18    K: Sub<Output = K> + Copy + Default,
19{
20    pub fn sub(&mut self, v: Vector<K>) {
21        if self.len() != v.len() {
22            panic!("The vector need to be on the same plan");
23        }
24
25        self.elements = self.iter().zip(v.iter()).map(|(&a, &b)| a - b).collect();
26    }
27}