DEV Community

Zhangwuji
Zhangwuji

Posted on

rust hashMap

  1. how to create hashmap in rust?
use std::collections::HashMap;
let a = HashMap::new();

let a2= HashMap::from([
("key","value"),
("key","value")
]);
Enter fullscreen mode Exit fullscreen mode
  1. how to insert ("key","value") into map instance?
let a2= HashMap::from([
("key","value"),
("key","value")
]);
a2.insert("dd","22")
Enter fullscreen mode Exit fullscreen mode

3.how to remove someone out of map instance?

 map.remove(&String::from("color2")); 
Enter fullscreen mode Exit fullscreen mode

4.how to update value of key?

     map.entry("test2").or_insert(200);
Enter fullscreen mode Exit fullscreen mode

5.how to get value of key?

 let val=  map.get("test2");
Enter fullscreen mode Exit fullscreen mode
  1. how to traverse map instance?
let mut a2= HashMap::from([
("key","value"),
("key","value")
]);

for (key,value) in &mut a2{
   value*="213"
};
Enter fullscreen mode Exit fullscreen mode

Top comments (0)