use std::ops::Add;
#[derive(Debug)]
struct Matrix<T> {
data: Vec<Vec<T>>,
}
impl<T: Add<Output = T> + Clone> Add for Matrix<T> {
type Output = Matrix<T>;
fn add(self, other: Matrix<T>) -> Matrix<T> {
assert_eq!(self.data.len(), other.data.len());
assert!(self.data.iter().zip(other.data.iter()).all(|(a, b)| a.len() == b.len()));
let mut result = Matrix {
data: vec![vec![T::default(); self.data[0].len()]; self.data.len()],
};
for (i, row) in self.data.iter().enumerate() {
for (j, element) in row.iter().enumerate() {
result.data[i][j] = element.clone() + other.data[i][j].clone();
}
}
result
}
}