This is a simplified version of code that I written for a leetcode question. In the problem from leetcode I have an immutable input like aux_vec_i32 below and I need to return something more complicated than in the sample code below, but the compiler errors are similar between the leetcode stuff I wrote and what is below. In the end hmap_for_data should hold the counts of unique counts of items in aux_vec_i32.
The current incantation of ‘&’, ‘mut’, ‘*’ and ‘as’ strewn in the code is the result of me fighting the compiler and not achieving success by following its suggestions.
use std::collections::HashMap;
fn main(){
struct Data{
the_data: i32,
}
let aux_vec_i32 = vec![1, 2, 3, 1];
let mut hmap_for_data: HashMap<i32, Data>=HashMap::new();
for i in &aux_vec_i32 {
match hmap_for_data.get(&mut aux_vec_i32[*i as usize]) {
None => {
hmap_for_data.insert(
aux_vec_i32[*i as usize],
Data {
the_data: 1,
}
);
}
Some(mut data_to_increment) => {
data_to_increment.the_data += 1;
}
}
}
}
Here is a link to the playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=535206613151ae0e4507296cf816d2a0.