- Super wild mindreading guess -
Your minimal reproducible example:
use std::collections::HashMap;
fn main() {
let nums: Vec<i32> = vec![1, 2, 3];
let k: i32 = 100;
let mut counts = HashMap::<&i32, &i32>::new();
for n in nums.iter() {
*counts.entry(*n).or_insert(0) -= -1;
*counts.entry(k - n).or_insert(0) -= 1;
}
println!("counts: {:?}", counts);
}
Solution:
use std::collections::HashMap;
fn main() {
let nums: Vec<i32> = vec![1, 2, 3];
let k: i32 = 100;
let mut counts = HashMap::<i32, i32>::new();
for n in nums.iter() {
*counts.entry(*n).or_insert(0) -= -1;
*counts.entry(k - n).or_insert(0) -= 1;
}
println!("counts: {:?}", counts);
}
counts: {99: -1, 97: -1, 1: 1, 98: -1, 2: 1, 3: 1}
The mistake you made was that you meant to write HashMap<i32,i32>
instead of HashMap<&i32,&i32>
.
It seems like a HashMap wants the types to be references
That is an incorrect statement. It only wants you to use references because you explicitly told it to use references with your HashMap<&i32, &i32>
type definition.