reimplementing [] based on Vec<T>.get() for safer return values

This commit is contained in:
jellyfishsh 2025-04-13 08:48:25 -07:00
parent f621d03f1a
commit 87ab2fefb4
2 changed files with 16 additions and 13 deletions

View File

@ -1,12 +0,0 @@
use super::Matrix;
use std::ops::Add;
impl Add for Matrix {
type Output = Matrix;
fn add(self, other: Matrix) -> Matrix {
}
}

View File

@ -42,7 +42,22 @@ impl Index<(usize, usize)> for Matrix {
//Since it returns an Option, its not as simple as a get //Since it returns an Option, its not as simple as a get
let row : Option<&Vec<i32>> = self.elements.get(index.0); //the 'T' in Option<T> from get is a reference let row : Option<&Vec<i32>> = self.elements.get(index.0); //the 'T' in Option<T> from get is a reference
//Now, we need to unpack the Option //Now, we need to unpack the Option
match row match row {
// Lifetimes???
// Turbofish is used in expressions
Some(row) => {
//Within THIS match:
let column : Option<&i32> = row.get(index.1);
match column {
Some(entry) => Some(entry),
None => &None,
}
},
None => &None,
}
} }
} }