Exerpad
🔥 0
0 XP
Exercise 8 · Chapter: HashMapsOptional+50 XP

Contains Key

Create a HashMap of favorite foods:

  • "Alice" likes "pizza"
  • "Bob" likes "tacos"

Check if "Alice" is in the map. If she is, print "Alice likes pizza!". Then check if "Carol" is in the map. If she's not, print "Carol not found!".

Expected output:

Alice likes pizza!
Carol not found!

Hints:

  • Use .contains_key("Alice") to check if a key exists
  • Use .get("Alice") with if let Some(food) to get the value
  • Use !map.contains_key("Carol") to check if a key is missing
rust
fn main() {
}
Output
Run your code to see the output here.