The question is: why can’t you get multiple mutable references to DIFFERENT values from a HashMap (code is below)?
Do I understand correctly that in this way the compiler prevents us from the phenomenon of mutable aliasing? Because it cannot statically guarantee that the resulting borrows point to different values?
Or simply protects us from multiple calls (with the same key) to methods that return mut borrow?
Or how, if we retrieve values using different keys, can we get the SAME value? How is it possible?
use std::collections::HashMap;
type PlayerID = i32;
#[derive(Debug, Default)]
struct Player {
score: i32,
}
fn main() {
let mut serv = HashMap::new();
serv.insert(2, Player::default());
let player_a = serv.get_mut(&32);
let player_b = serv.get_mut(&32);
dbg!(player_a, player_b);
}
I marked as a solution an answer that provides a link to a page that answers the question “why” the problem I described exists at all. This interested me more than possible ways to solve it.