Here is a more idiomatic implementation for File:
impl File {
fn row(&self, index: usize) -> Option<&Row> {
self.rows.get(index)
}
fn row_mut(&mut self, index: usize) -> Option<&mut Row> {
self.rows.get_mut(index)
}
}
Items of note here:
- Your implementation would panic if
indexis out of bounds. The idiomatic way of handling this is to return an Option, whichgetandget_mutallow you to get for free. - Using u16 does not make much sense, as Vec is indexed using usize. Using
u16is arbitrary here unless you really want to provide hard-coded limitations. In that case, I wouldn’t rely on the type’s max value but a constant instead that would make the intent clearer.