I'm writing a server implementation for a multiplayer game (without the networking part atm). The server owns two hashmaps:
disconnected, of type
It has a
connect method of type
(&mut self, character_id: &CharacterId) -> Result<...> that looks up the id in the disconnected map, and moves it into the connected map. So far, so good.
The issue is that it also immutably borrows the
CharacterState from one of the maps, and has to call another server method,
join_scene, that mutably borrows
self and immutably borrows that character state.
That doesn't work:
let state = self.disconnected.get(character_id).unwrap(); // immutable borrow occurs here self.join_scene(state); // cannot borrow *self as mutable because it is also borrowed as immutable
I understand the issue: if
join_scene mutably borrows
self, then it could change the
CharacterState immutably borrowed by
state, which breaks Rust's rules.
I'm wondering what's the idiomatic way to handle situations like this:
clone()a part of the state: I will probably only ever need just two strings from
CharacterState, realistically it won't be a performance issue
Rcor the like: seems like an overkill, but I'm just learning Rust so I might be wrong and this is an acceptable solution
- a different approach to the design: any suggestions about how to avoid the issue altogether?
- some other trick