Hello everyone! I am relatively pretty new to Rust! I'm find this language beautiful to read. I took on a ray tracing challenge a couple weeks ago, and I'm now facing skill issue with traits.
#[derive(Debug, Clone, PartialEq)]
struct Matrix<T> {
row: usize,
col: usize,
data: Vec<T>,
}
impl<T> Matrix<T> {
fn new(row: usize, col: usize, data: Vec<T>) -> Self {
Self { row, col, data }
}
fn at(&self, row: usize, col: usize) -> Option<&T> {
if row > self.row || col > self.col {
return None;
}
self.data.get((row * self.col) * col)
}
}
impl<T> Mul for Matrix<T>
where
T: std::ops::Mul + Copy,
{
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
let mut res = Matrix::new(self.row, self.col, vec![]);
for r in 0..self.row {
for c in 0..self.col {
let mut intersection_sum = 0;
for i in 0..self.row {
let intersection_one = self.at(r, i).unwrap().clone(); // type is T
let intersection_two = self.at(i, c).unwrap().clone(); // type is T
let mul = intersection_one * intersection_two: // type is <T as Mul<Self>>Output
intersection_sum += mul; // cannot add-assign `<T as std::ops::Mul>::Output` to `{integer}
// the trait `std::ops::AddAssign<<T as std::ops::Mul>::Output> is not implemented for {integer}
}
}
}
}
}
What I don't understand is that the type of intersection_one
is T
, and intersection_two
is T
. But when multiplying, it becomes <T as Mul<Self>>Output
. I'm reading this as Mul's Output is T?
For intersection_sum += mul
, I understand that there's not trait that defines how += behaves, so do I simply create a new impl AddAssign for Matrix? But Matrix isn't the intended type, it should be T?
I would really like feedback here. I'm trying to go back to the basics but I'm not sure how to advance from here. If anyone could give tips or resources to traits, that would be really appreciated!