Hey there!
I'm a new rust developer. I'm a University student, and I'm trying to branch out to languages outside of the C-sphere that universities tend to be stuck in.
Coming from a C++ background, I am finding the ownership rules of rust the hardest to manage.
I have two methods:
fn event_handler(&mut self){
for event in self.event_pump.poll_iter(){
self.is_running = self.event_window(event);
}
}
and
fn event_window(&self, event: sdl2::event::Event)-> bool {
let mut running = self.is_running;
match event {
sdl2::event::Event::Quit{ .. } => running = false,
_ => {},
}
return running;
}
I receive the error
C:/Users/plays/.cargo/bin/cargo.exe run --package Game_Prototype --bin Game_Prototype
Compiling Game_Prototype v0.1.0 (G:\ProgrammingModding\GameEngine2D)
error[E0502]: cannot borrow*self
as immutable becauseself.event_pump
is also borrowed as mutable
--> src\game_context.rs:26:31
|
25 | for event in self.event_pump.poll_iter(){
| --------------- - mutable borrow ends here
| |
| mutable borrow occurs here
26 | self.is_running = self.event_window(event);
| ^^^^ immutable borrow occurs here
I understand why this error occurs. event_window() receives an immutable reference to self, whereas self.event_pump.poll_iter() is a mutable reference, and you cannot have a mutable reference borrow before an immutable reference borrow.
I'm not sure how I'm supposed to structure these methods in the way that rust's ownership rules would like me to, however. I tried changing event_window() to receive a mutable reference, but rust does not allow for two mutable borrows, either.
event_window() is supposed to iterate over the event queue of my program, and then test each given event for a list of event types, each split into their own functions for readability. (I.e., window events will have their own function, and key press events will have their own function)
How is one supposed to create methods that act upon the properties of a given class if the self keyword borrows the object, and then owns that object for the rest of the scope? It restricts me to acting upon only one property at a time, which seems horribly inefficient in terms of my work load..