官术网_书友最值得收藏!

  • Rust Standard Library Cookbook
  • Jan Nils Ferner Daniel Durante
  • 518字
  • 2021-08-27 19:45:12

How to do it...

  1. In the folder src/bin, create a file called hashmap.rs.
  2. Add the following code, and run it with cargo run --bin hashmap:
1   use std::collections::HashMap; 
2
3 fn main() {
4 // The HashMap can map any hashable type to any other
5 // The first type is called the "key"
6 // and the second one the "value"
7 let mut tv_ratings = HashMap::new();
8 // Here, we are mapping &str to i32
9 tv_ratings.insert("The IT Crowd", 8);
10 tv_ratings.insert("13 Reasons Why", 7);
11 tv_ratings.insert("House of Cards", 9);
12 tv_ratings.insert("Stranger Things", 8);
13 tv_ratings.insert("Breaking Bad", 10);
14
15 // Does a key exist?
16 let contains_tv_show = tv_ratings.contains_key("House of
Cards");
17 println!("Did we rate House of Cards? {}", contains_tv_show);
18 let contains_tv_show = tv_ratings.contains_key("House");
19 println!("Did we rate House? {}", contains_tv_show);
20
21 // Access a value
22 if let Some(rating) = tv_ratings.get("Breaking Bad") {
23 println!("I rate Breaking Bad {} out of 10", rating);
24 }
25
26 // If we insert a value twice, we overwrite it
27 let old_rating = tv_ratings.insert("13 Reasons Why", 9);
28 if let Some(old_rating) = old_rating {
29 println!("13 Reasons Why's old rating was {} out of 10",
old_rating);
30 }
31 if let Some(rating) = tv_ratings.get("13 Reasons Why") {
32 println!("But I changed my mind, it's now {} out of 10",
rating);
33 }
34
35 // Remove a key and its value
36 let removed_value = tv_ratings.remove("The IT Crowd");
37 if let Some(removed_value) = removed_value {
38 println!("The removed series had a rating of {}",
removed_value);
39 }
40
41 // Iterating accesses all keys and values
42 println!("All ratings:");
43 for (key, value) in &tv_ratings {
44 println!("{}\t: {}", key, value);
45 }
46
47 // We can iterate mutably
48 println!("All ratings with 100 as a maximum:");
49 for (key, value) in &mut tv_ratings {
50 *value *= 10;
51 println!("{}\t: {}", key, value);
52 }
53
54 // Iterating without referencing the HashMap moves its
contents
55 for _ in tv_ratings {}
56 // tv_ratings is not usable anymore

If you don't need to access both keys and values at the same time, you can iterate over either individually:

58    // Like with the other collections, you can preallocate a size
59 // to gain some performance
60 let mut age = HashMap::with_capacity(10);
61 age.insert("Dory", 8);
62 age.insert("Nemo", 5);
63 age.insert("Merlin", 10);
64 age.insert("Bruce", 9);
65
66 // Iterate over all keys
67 println!("All names:");
68 for name in age.keys() {
69 println!("{}", name);
70 }
71
72 // Iterate over all values
73 println!("All ages:");
74 for age in age.values() {
75 println!("{}", age);
76 }
77
78 // Iterate over all values and mutate them
79 println!("All ages in 10 years");
80 for age in age.values_mut() {
81 *age += 10;
82 println!("{}", age);
83 }
84

You can use the entry API to assign default values to keys if they're not yet in the HashMap:

87    {
88 let age_of_coral = age.entry("coral").or_insert(11);
89 println!("age_of_coral: {}", age_of_coral);
90 }
91 let age_of_coral = age.entry("coral").or_insert(15);
92 println!("age_of_coral: {}", age_of_coral);
93 }
主站蜘蛛池模板: 永胜县| 全州县| 武定县| 赤城县| 山阳县| 通辽市| 白沙| 望谟县| 吉安市| 贵德县| 永春县| 环江| 新营市| 永嘉县| 大姚县| 孟连| 峨山| 侯马市| 桂平市| 洛川县| 五原县| 泰兴市| 永修县| 瑞昌市| 穆棱市| 长兴县| 金门县| 平阳县| 大安市| 齐齐哈尔市| 新蔡县| 安国市| 清河县| 宜宾县| 明光市| 海南省| 宜昌市| 翼城县| 四会市| 太仆寺旗| 长白|