I am trying out some examples in the The Rust Programming Language book, and have the following code snippet:
fn main() {
let mut map: HashMap<&str, i32, RandomState> = HashMap::new();
let hello: String = String::from("hello");
map.insert(&hello, 100);
println!("{:?}", map); //{"hello": 100}
let first_hello_score: Option<&i32> = map.get("hello"); // This compiles
let hello_score: Option<&i32> = map.get(&hello); // This does not compile
}
On running cargo check, I see:
error[E0277]: the trait bound `&str: Borrow<String>` is not satisfied
--> src/main.rs:26:27
|
26 | let hello_score = map.get(&hello);
| ^^^ the trait `Borrow<String>` is not implemented for `&str`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0277`.
Could someone explain why this happens?