-2

I have the following code where nums is a Vec<i32>, k is an i32, and counts is a HashMap<&i32, &i32>:

for n in nums.iter() {
    *counts.entry(n).or_insert(0) -= -1;
    *counts.entry(k - n).or_insert(0) -= 1;
}

It seems like a HashMap wants the types to be references, which complicates things. As k is an i32 and n is an &i32 I would need to do something like:

*counts.entry(&(k - *n)).or_insert(0) -= 1;

However, this won't work because the lifetime of &(k - *n).

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
eof
  • 413
  • 4
  • 14
  • It's hard to answer your question because it doesn't include a [MRE]. We can't tell what crates (and their versions), types, traits, fields, etc. are present in the code. It would make it easier for us to help you if you try to reproduce your error on the [Rust Playground](https://play.rust-lang.org) if possible, otherwise in a brand new Cargo project, then [edit] your question to include the additional info. There are [Rust-specific MRE tips](//stackoverflow.com/tags/rust/info) you can use to reduce your original code for posting here. Thanks! – Shepmaster May 10 '22 at 13:34
  • 1
    Please [edit] your question and paste the exact and entire error that you're getting — that will help us to understand what the problem is so we can help best. Sometimes trying to interpret an error message is tricky and it's actually a different part of the error message that's important. Please use the message from running the compiler directly, not the message produced by an IDE, which might be trying to interpret the error for you. – Shepmaster May 10 '22 at 13:34
  • The argument to `entry()` is of type `K`, not `&K`. In other words, you should pass the actual keys, not references to keys. If you get an error that references are expected, then maybe your key type is `&i32`. In almost all circumstances, it makes more sense to use `i32` as the key type. – Sven Marnach May 10 '22 at 13:39
  • Maybe simply using `for &n in nums.iter()` (note the ampersand) fixes the first code snippet – this should make sure that both `n` and `k` are `i32`s. – Sven Marnach May 10 '22 at 13:41
  • 1
    As you've asked it, your question would be answered by the answers of [Is there any way to return a reference to a variable created in a function?](https://stackoverflow.com/q/32682876/155423). If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster May 10 '22 at 13:42
  • 1
    You also say "an integer hashmap" but that the types of the hashmap are references. This is a double reason you should provide a [MRE] and error text. – Shepmaster May 10 '22 at 13:44
  • 1
    I'm super confused by everything in this question :D – Finomnis May 10 '22 at 13:46
  • Why `-= -1`? Wouldn't that just be `+= 1`? – Finomnis May 10 '22 at 13:52
  • If you write all the types of your variables in the text already, why not just include them in the code example? You are just making us the work of having to manually copy paste each of them if we want to reproduce your problem. – Finomnis May 10 '22 at 14:00

1 Answers1

2

- 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.

Finomnis
  • 18,094
  • 1
  • 20
  • 27
  • uhm, are we sure he do not have a `<&i32, &i32>`? and hence the problem? – Netwave May 10 '22 at 13:59
  • @Netwave added my recreation of his minimal reproducible example to make it clearer. I guess I confused you there because I skipped the step and went directly to the solution – Finomnis May 10 '22 at 14:02