reimplementing [] based on Vec<T>.get() for safer return values
This commit is contained in:
parent
f621d03f1a
commit
87ab2fefb4
@ -1,12 +0,0 @@
|
|||||||
use super::Matrix;
|
|
||||||
use std::ops::Add;
|
|
||||||
|
|
||||||
impl Add for Matrix {
|
|
||||||
type Output = Matrix;
|
|
||||||
|
|
||||||
fn add(self, other: Matrix) -> Matrix {
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -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,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user