cannot borrow as mutable, as it is behind a `&` reference

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 index is out of bounds. The idiomatic way of handling this is to return an Option, which get and get_mut allow you to get for free.
  • Using u16 does not make much sense, as Vec is indexed using usize. Using u16 is 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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)