use std::ops::Add;
pub struct Matrix4 {
matrix: [[f64; 4]; 4]
}
impl Index<usize> for Matrix4 {
type Output = [f64; 4];
fn index(&self, i: usize) -> &[f64; 4] {
&self.matrix[i]
}
}
impl Add<Matrix4> for Matrix4 {
type Output = Matrix4;
fn add(&self, rhs: &Matrix4) -> Matrix4 {
Matrix4 {
matrix: [
[self[0][0] + rhs[0][0], self[0][1] + rhs[0][1], self[0][2] + rhs[0][2], self[0][3] + rhs[0][3]],
[self[1][0] + rhs[1][0], self[1][1] + rhs[1][1], self[1][2] + rhs[1][2], self[1][3] + rhs[1][3]],
[self[2][0] + rhs[2][0], self[2][1] + rhs[2][1], self[2][2] + rhs[2][2], self[2][3] + rhs[2][3]],
[self[3][0] + rhs[3][0], self[3][1] + rhs[3][1], self[3][2] + rhs[3][2], self[3][3] + rhs[3][3]]
]
}
}
}
Код выше не скомпилируется. В нем говорится:
error[E0053]: method `add` has an incompatible type for trait
--> src/lib.rs:10:5
|
10 | fn add(&self, rhs: &Matrix4) -> Matrix4 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `Matrix4`, found `&Matrix4`
|
= note: expected fn pointer `fn(Matrix4, Matrix4) -> Matrix4`
found fn pointer `fn(&Matrix4, &Matrix4) -> Matrix4`
( Детская площадка )
Просто FYI, это существующая черта, которую ржавчина использует для наложения операторов. Цель состоит в том, чтобы быть в состоянии сделать это в ржавчине, где A и B являются структурами, которые представляют матрицы:
A + B