In Rust, you update a value in a HashMap by calling insert() with an existing key:
use std::collections::HashMap;
let mut scores: HashMap<&str, i32> = HashMap::new();
scores.insert("Alice", 95);
scores.insert("Bob", 87);
scores.insert("Alice", 99); // update: Alice is now 99
scores.insert("Charlie", 72); // add new entry
The insert() method both creates new entries and updates existing ones. You can also use the entry API to update only existing keys:
scores.entry("Alice").and_modify(|v| *v = 99); // updates Alice if exists
Assignment
Follow the Coding Tutorial and update Alice's score to 99!
Hint
Use scores.insert("Alice", 99).
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check