创建一个HashMap
类似于 vector,哈希 map 是同质的:所有的键必须是相同类型,值也必须都是相同类型。

  1. use std::collections::HashMap;
  2. let mut scores = HashMap::new();
  3. scores.insert(String::from("Blue"), 10);
  4. scores.insert(String::from("Yellow"), 50);
  5. --------------------------------------------
  6. use std::collections::HashMap;
  7. let teams = vec![String::from("Blue"), String::from("Yellow")];
  8. let initial_scores = vec![10, 50];
  9. let scores: HashMap<_, _> = teams.iter().zip(initial_scores.iter()).collect();

[

哈希 map 和所有权

](https://kaisery.github.io/trpl-zh-cn/ch08-03-hash-maps.html#%E5%93%88%E5%B8%8C-map-%E5%92%8C%E6%89%80%E6%9C%89%E6%9D%83)`i32` 这样的实现了 Copy trait 的类型,其值可以拷贝进哈希 map
String 这样拥有所有权的值,其值将被移动而哈希 map 会成为这些值的所有者[

  1. use std::collections::HashMap;
  2. let field_name = String::from("Favorite color");
  3. let field_value = String::from("Blue");
  4. let mut map = HashMap::new();
  5. map.insert(field_name, field_value);
  6. // 这里 field_name 和 field_value 不再有效,
  7. // 尝试使用它们看看会出现什么编译错误!

](https://kaisery.github.io/trpl-zh-cn/ch08-03-hash-maps.html#%E5%93%88%E5%B8%8C-map-%E5%92%8C%E6%89%80%E6%9C%89%E6%9D%83)

访问哈希map的值

  1. use std::collections::HashMap;
  2. let mut scores = HashMap::new();
  3. scores.insert(String::from("Blue"), 10);
  4. scores.insert(String::from("Yellow"), 50);
  5. let team_name = String::from("Blue");
  6. let score = scores.get(&team_name);

遍历哈希map的值

  1. use std::collections::HashMap;
  2. let mut scores = HashMap::new();
  3. scores.insert(String::from("Blue"), 10);
  4. scores.insert(String::from("Yellow"), 50);
  5. for (key, value) in &scores {
  6. println!("{}: {}", key, value);
  7. }

[

更新哈希 map

](https://kaisery.github.io/trpl-zh-cn/ch08-03-hash-maps.html#%E6%9B%B4%E6%96%B0%E5%93%88%E5%B8%8C-map)[

覆盖一个值

](https://kaisery.github.io/trpl-zh-cn/ch08-03-hash-maps.html#%E8%A6%86%E7%9B%96%E4%B8%80%E4%B8%AA%E5%80%BC)[

  1. use std::collections::HashMap;
  2. let mut scores = HashMap::new();
  3. scores.insert(String::from("Blue"), 10);
  4. scores.insert(String::from("Blue"), 25);
  5. println!("{:?}", scores);

](https://kaisery.github.io/trpl-zh-cn/ch08-03-hash-maps.html#%E6%9B%B4%E6%96%B0%E5%93%88%E5%B8%8C-map)[ ](https://kaisery.github.io/trpl-zh-cn/ch08-03-hash-maps.html#%E8%A6%86%E7%9B%96%E4%B8%80%E4%B8%AA%E5%80%BC)[

只在键没有对应值时插入

](https://kaisery.github.io/trpl-zh-cn/ch08-03-hash-maps.html#%E5%8F%AA%E5%9C%A8%E9%94%AE%E6%B2%A1%E6%9C%89%E5%AF%B9%E5%BA%94%E5%80%BC%E6%97%B6%E6%8F%92%E5%85%A5)[

](https://kaisery.github.io/trpl-zh-cn/ch08-03-hash-maps.html#%E8%A6%86%E7%9B%96%E4%B8%80%E4%B8%AA%E5%80%BC)[```rust use std::collections::HashMap;

let mut scores = HashMap::new(); scores.insert(String::from(“Blue”), 10);

scores.entry(String::from(“Yellow”)).or_insert(50); scores.entry(String::from(“Blue”)).or_insert(50);

println!(“{:?}”, scores);

  1. ](https://kaisery.github.io/trpl-zh-cn/ch08-03-hash-maps.html#%E6%9B%B4%E6%96%B0%E5%93%88%E5%B8%8C-map)[<a name="79a8ebab"></a>
  2. #### 根据旧值更新一个值
  3. ](https://kaisery.github.io/trpl-zh-cn/ch08-03-hash-maps.html#%E6%A0%B9%E6%8D%AE%E6%97%A7%E5%80%BC%E6%9B%B4%E6%96%B0%E4%B8%80%E4%B8%AA%E5%80%BC)[<a name="d41d8cd9-1"></a>
  4. ####
  5. ](https://kaisery.github.io/trpl-zh-cn/ch08-03-hash-maps.html#%E8%A6%86%E7%9B%96%E4%B8%80%E4%B8%AA%E5%80%BC)如果是第一次看到某个单词,就插入值 `0`[<a name="d41d8cd9-2"></a>
  6. ####
  7. ](https://kaisery.github.io/trpl-zh-cn/ch08-03-hash-maps.html#%E8%A6%86%E7%9B%96%E4%B8%80%E4%B8%AA%E5%80%BC)[```rust
  8. use std::collections::HashMap;
  9. let text = "hello world wonderful world";
  10. let mut map = HashMap::new();
  11. for word in text.split_whitespace() {
  12. let count = map.entry(word).or_insert(0);
  13. *count += 1;
  14. }
  15. println!("{:?}", map);

](https://kaisery.github.io/trpl-zh-cn/ch08-03-hash-maps.html#%E6%9B%B4%E6%96%B0%E5%93%88%E5%B8%8C-map)