Here is the full code in question.
I'm making an app using the Nannou framework. I would like to keep a reference to the selected cell in my model, but I'm fighting the borrow checker on this one. It complains that I am borrowing the grid
value after move, and that I cannot return a value referencing data owned by the current function. How can I make this work?
#[derive(Default)]
struct Grid {
state: [[Cell; 9]; 9],
}
struct Model<'a> {
grid: Grid,
selected: &'a mut Cell,
}
impl<'a> Model<'a> {
fn select(&'a mut self, x: usize, y: usize) {
self.selected = &mut self.grid.state[x][y];
}
}
fn model(app: &App) -> Model<'static> {
let _window = app.new_window()
.view(view)
.key_pressed(key_pressed)
.build()
.unwrap();
let mut grid = Grid::default();
Model {
grid,
selected: &mut grid.state[0][0],
}
}