I have this little program.
use std::collections::HashMap;
fn main() {
let mut map: HashMap<&str, u32> = HashMap::new();
let new = "TOMATO".to_string();
if !map.contains_key(&new) {}
}
When I try to compile this program, I get an error that I believe is quite confusing.
error[E0277]: the trait bound `&str: std::borrow::Borrow<std::string::String>` is not satisfied
--> src/main.rs:5:13
|
5 | if !map.contains_key(&new) {}
| ^^^^^^^^^^^^ the trait `std::borrow::Borrow<std::string::String>` is not implemented for `&str`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0277`.
I have already resolved this and there were two ways to resolve this.
- Use
HashMap<String, _>
signature instead ofHashMap<&str, _>
- Replace,
if !map.contains_key(&new) {}
withif !map.contains_key(new.as_str()) {}
.
I am wondering if anyone else also found the compiler error to be confusing and maybe if we can do anything to improve it? Playground link