I don't think I understand "the rust way" to make a game engine

71 points Valyn_Tyler

I'm doing some low level stuff in OpenGL for a school project and wanted to use rust.

Right now, I'm trying to make a simple button structure that fires an event when pressed and (much like most people learning rust, I'm told) I'm struggling against the borrow checker as I'd like to both mutate the state from inside the button callback, while still being able to read that state from the outside.

Hope that was an adequate explanation of what I'm trying to do, anyways, here's a simplified code snippet of what I'm trying to do:

\\\\`

fn main() {
let x: i32 = 0;
let button\_callback = Box::new(|| {
println!("{}", x);
x += 1; // error here
});
// game loop
{
/\ if button.is\_pressed() \/ {
button\_callback()
}
// do stuff with x
// x = example.random\_assignment();
}
}

\\\\`