use std::collections::HashMap;
fn count_words(str: &str) -> HashMap<&str, i32> {
let mut map = HashMap::new();
for word in str.split_whitespace() {
let count = map.entry(word).or_insert(0);
*count += 1;
}
return map;
}
fn main () {
let mut scores = HashMap::new();
scores.insert(String::from("jack"), 96.5);
scores.insert(String::from("ruby"), 23.5);
let teams = vec![String::from("red"), String::from("green")];
let team_scores = vec![96.4, 94.5];
let mut new_scores: HashMap<_, _> = teams.iter().zip(
team_scores.iter()
).collect();
println!("{:?}", new_scores);
let team_name = String::from("red");
let team_score = new_scores.get(&team_name);
match team_score {
Some(s) => println!("{}", s),
None => println!("No score"),
}
for (k, v) in &new_scores {
println!("{}: {}", k, v);
}
let name = String::from("jack");
let val = 76.8;
new_scores.insert(&name, &val);
println!("new_scores: {:?}", new_scores);
let mut cscores = HashMap::new();
cscores.insert(String::from("Blue"), 10.5);
cscores.insert(String::from("Yellow"), 20.5);
println!("{:?}", cscores);
cscores.insert(String::from("Blue"), 97.6);
println!("{:?}", cscores);
let e = cscores.entry(String::from("Blue"));
println!("{:?}", e);
e.or_insert(65.9);
let count_result = count_words("Hello world Hello rust");
println!("{:#?}", count_result);
}