I was playing around with handling things in rust and I have run into a situation I can't explain. In the code below the static vector works fine, but the static hashmap will not compile. Why is there a difference in how they are handled and is there a way to make the hashmap work?
use std::collections::HashMap;
fn foo(i: u64) {
static mut x: Vec<u64> = Vec::new(); // works okay
unsafe {x.push(i);}
println!("{}", unsafe{x.len()});
}
fn bar(i: u64) {
static mut x: HashMap<u64, u64> = HashMap::default(); // not allowed
unsafe {x.insert(i, 0);}
println!("{}", unsafe{x.len()});
}
fn main() {
foo(1);
foo(2);
foo(3);
bar(1);
bar(2);
bar(3);
}