I have this code:
struct Player {}
impl Player {
fn update(&mut self) {}
}
struct GameBoard {
p1: Player,
p2: Player,
}
impl GameBoard {
fn update(&mut self) {}
fn update_current_player(&mut self, p1_turn: bool) {
if p1_turn {
self.p1.update()
} else {
self.p2.update();
}
self.update();
if p1_turn {
self.p1.update()
} else {
self.p2.update();
}
}
}
I'd like to not type out the if
statement every time I want to update the current player. In other languages I've used, I would write something like this:
struct Player {}
impl Player {
fn update(&mut self) {}
}
struct GameBoard {
p1: Player,
p2: Player,
}
impl GameBoard {
fn update(&mut self) {}
fn update_current_player(&mut self, p1_turn: bool) {
let p = if p1_turn { &mut self.p1 } else { &mut self.p2 };
p.update();
self.update();
p.update();
}
}
But this annoys the borrow checker:
error[E0499]: cannot borrow `*self` as mutable more than once at a time
--> src/lib.rs:18:9
|
15 | let p = if p1_turn { &mut self.p1 } else { &mut self.p2 };
| ------------ first mutable borrow occurs here
...
18 | self.update();
| ^^^^ second mutable borrow occurs here
19 |
20 | p.update();
| - first borrow later used here
There must be some way not to have to type out the if
statement every time. I would also prefer not to break the if
statement out into a function that takes p1_turn
, because writing out the function call is still fairly long. Does anyone have suggestions?
I understand why this pattern is unsafe if you're accessing data inside an Enum
or on the heap, since the call to self.update()
might invalidate the reference, but in this case the reference can't possibly be made invalid by any code in self.update()
because it's just referring to a field of the struct.