use std::collections::HashMap;
#[allow(dead_code)]
fn basic_operation() {
let mut scores = HashMap::new();
scores.insert("green", 10);
scores.insert("red", 20);
scores.insert("red", 50);
let value1 = scores.get("red").copied().unwrap_or(0);
let value2 = scores.get("red").unwrap_or(&0);
println!("{:#?}", value1);
println!("{:#?}", value2);
for (key, value) in scores {
println!("key: {}, value: {}", key, value);
}
}
#[allow(dead_code)]
fn hashmap_and_ownership() {
let mut hashmap1 = HashMap::new();
let mut hashmap2 = HashMap::new();
let key1 = "np";
let value1 = "+977";
hashmap1.insert(key1, value1);
hashmap1.entry("us").or_insert("+1");
let key2 = String::from("hello");
let value2 = String::from("world");
hashmap2.insert(key2, value2);
println!("Key1: {}", key1);
println!("value1: {}", value1);
}
fn update_hash_map() {
let text = "hello world wonderful world";
let mut map = HashMap::new();
for word in text.split_whitespace() {
let count = map.entry(word).or_insert(0);
*count = *count + 1;
}
println!("{:#?}", map);
}
#[allow(dead_code)]
pub fn collection() {
update_hash_map();
}