I'm new to rust and hope you can explain what will happen if you assign to dereferenced self?
Does it change value in memory by the same reference, or it will destroy value in memory and create new with new reference?
struct X {
v: f32,
}
impl X {
fn new() -> X {
X {
v: 0.0,
}
}
fn do_it(&mut self) {
*self = X::new();
}
}
}
I'm learning rust by following game creation tutorial and in it if player starts a new game i need to revert GameState to default.
impl GS {
fn new() -> GS {
GS {
frame_time: 0.0,
player: Player::new(),
obstacle: Obstacle::new(),
interface_state: InterfaceState::MainMenu,
}
}
fn main_menu(&mut self, ctx: &mut BTerm) {
ctx.cls();
ctx.print_centered(WINDOW_HEIGHT / 2 - 5, "Menu");
ctx.print_centered(WINDOW_HEIGHT / 2 - 4, "New Game - Q");
if Some(VirtualKeyCode::Q) == ctx.key {
*self = GS::new();
self.interface_state = InterfaceState::Playing;
}
}
}