1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
us std::collections::HashMap;
fn main() { let mut scoures: HashMap<String,i32> = HahsMap::new(); scores.insert(String::from("Blue"),10); scores.insert(String::from("Red"),20);
let keys = vec![String::from("Blue"),String::from("Red")]; let value = vec![10,20]; let scores: HashMap<_,_> = keys.iter().zip(values.iter()).collect(); let k = String::from("Blue"); if let Some(v) = scores.get(&k) { println!("Blue = {}",v); } let k = String::from("yellow"); let v = scores.get(&k); match v { Some(value) => println!("Blue = {}",value), None => println!("None"), } for (key,value) in &scores { println!("{},{}",key,value); } let mut ss = HashMap::new(); ss.insert(String::from("one"),1); ss.insert(String::from("two"),2); ss.insert(String::from("three"),3); ss.insert(String::from("one"),3); println!("{:?}",ss);
let mut ss1 = HashMap::new(); ss1.insert(String::from("one"),1); ss1.insert(String::from("two"),2); ss1.insert(String::from("three"),3); ss1.entry(String::from("one")).or_insert(3); println!("{:?}",ss1); let text = "Hello world wonderful world"; let mut map = HashMap::new(); for word in text.split_whitespace() { let mut count = map.entry(word).or_insert(0); *count += 1; } }
|