- entry
- new
- or_insert
Rust hash update value
- Using the or_insert method we can update a value and insert a default value in case the key did not exist.
examples/hashes/update-hash-value/src/main.rs
use std::collections::HashMap; fn main() { let mut counter = HashMap::new(); *counter.entry("apple").or_insert(0) += 1; println!("{:#?}", counter); *counter.entry("banana").or_insert(0) += 1; println!("{:#?}", counter); *counter.entry("apple").or_insert(0) += 1; println!("{:#?}", counter); }
{ "apple": 1, } { "banana": 1, "apple": 1, } { "banana": 1, "apple": 2, }
examples/hashes/updating-values/src/main.rs
use std::collections::HashMap; fn main() { let mut grades = HashMap::new(); grades.insert(String::from("Joe"), 70); // create println!("{}", grades["Joe"]); grades.insert(String::from("Joe"), 71); // overwite println!("{}", grades["Joe"]); grades.entry(String::from("Jane")).or_insert(80); // inserts if the key did not exist println!("{}", grades["Jane"]); grades.entry(String::from("Jane")).or_insert(81); // does not change the value println!("{}", grades["Jane"]); // inplace change of value (defaults to 0 if key does not exist) let value = grades.entry(String::from("Joe")).or_insert(0); *value += 1; println!("{}", grades["Joe"]); // the same, no need for temporary variable *grades.entry(String::from("Joe")).or_insert(0) += 1; println!("{}", grades["Joe"]); // This did not exists so defaults to 0 *grades.entry(String::from("George")).or_insert(0) += 1; println!("{}", grades["George"]); }