I have the following struct Grid
where I would like to have a mutable get
function which returns mutable references to the underlying Cell
s:
MWE here
struct Cell {
c: i64,
}
struct Grid {
a: [Cell; 3],
}
impl Grid {
fn cell_i(&mut self, i: usize) -> &mut Cell {
return &mut self.a[i];
}
fn neighbors(&mut self, _i: usize) -> (&mut Cell, &mut Cell) {
return (self.cell_i(0), self.cell_i(2));
}
}
fn main() {
let a = &mut Grid {
a: [Cell { c: 3 }, Cell { c: 4 }, Cell { c: 5 }],
};
let _bs = a.neighbors(1);
}
I am just puzzeled as a beginner, how to solve this?
I need both neighbor cells mutable returned from neighbors
, to make computations on them.
I could return immutable
but then I cannot turn them into &mut Cell
s because that does not exist (undefined behavior). Whats the common idiom here?