You are on page 1of 2

use std::collections::HashMap;

// collect() demo
pub fn demo11() {
   let mut scores = HashMap::new();

   scores.insert(String::from("Blue"), 10);

   println!("{:?}", scores);

   let teams = vec![String::from("Blue"), String::from("Yellow")];


   let nums = vec![10, 50];

   let scores: HashMap<_, _> = teams.iter().zip(nums.iter()).collect();

   println!("{:?}", scores);
}

// ownship demo
pub fn demo12() {
   let key = String::from("favorite color");

   let value = String::from("blue");

   let mut map = HashMap::new();


   map.insert(&key, &value);

   println!("{:?}", map);
   println!("key is {}, value is {}.", key, value); // borrow of moved value(if
not &).
}

// get() demo
pub fn demo13() {
   let mut scores = HashMap::new();
   scores.insert(String::from("Blue"), 10);
   scores.insert(String::from("Yellow"), 50);

   let acq1 = String::from("Blue");


   let acq2 = String::from("Pink");

   let res1 = scores.get(&acq1);


   let res2 = scores.get(&acq2);

   match res1 {
       Some(v) => println!("Team {} scores {}.", acq1, v),
       None => println!("Team {} doesn't exist.", acq1)
  }

   match res2 {
       Some(v) => println!("Team {} scores {}.", acq2, v),
       None => println!("Team {} doesn't exist.", acq2)
  }

// traverse demo
pub fn demo14() {
   let mut scores = HashMap::new();
   scores.insert(String::from("Blue"), 10);
   scores.insert(String::from("Yellow"), 50);

   for (k, v) in scores {


       println!("{}, {}.", k, v);
  }
}

You might also like