I have a block of code
let mut current_room = None;
for (ref room_id, ref poker_room) in &self.planning_pokers {
if poker_room.has_player(user_id.clone()) {
current_room = Some(room_id.clone());
break
}
}
match current_room {
Some(room_id) => {
self.planning_pokers.get_mut(&room_id.clone()).unwrap().remove_player(user_id.clone());
if self.planning_pokers.is_empty() {
self.planning_pokers.remove(&room_id.clone());
}
},
None => (),
_ => ()
}
println!("Player {} joined room {}", join_room.room_id.clone(), user_id.clone());
if self.planning_pokers.contains_key(&join_room.room_id.clone()) {
}
This fails to compile due to a mutable and immutable borrow conflict. The conflict in question pertains to me setting the value of current_room
to Some(room_id.clone())
. If I instead do Some(room_id.clone().to_string())
everything works as expected. Why is this the case? And why does the rust compiler give me an error that is seemingly unrelated?
For reference this is the error:
cannot borrow
self.planning_pokers
as mutable because it is also borrowed as immutable