Problem with HashMap contains_key()

Context for the code:

let skipped = HashMap::new();
let mut freefams = 0; 
reservations.keys().sorted().for_each(|r#type|{
        let v = &reservations[r#type]; 
        if v > &(0 as u64) && usedtypes.contains_key(r#type){
            skipped.entry(r#type).or_insert(v);
            freefams += 1; 
        }
});

I get an error stating "the trait Borrow<&str> is not implemented for &std::string::String" with the line:

usedtypes.contains_key(r#type)

For reference, usedtypes is a HashMap with key of type String and value of type String.
Thanks for your time and help.

Can you please double check that the key type is String and not &String. You can do this by adding a type annotation:

let usedtypes: HashMap<String, String> = ...;
2 Likes

Ok that worked for me. I added the type annotation and I was able to use .to_string() to get it to work. Thanks.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.