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!("{} : {}", 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!("{} : {}", 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 }
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.124.21