I'd rather explain _ than 'static at this point tbh

This commit is contained in:
Carol (Nichols || Goulding) 2016-09-19 11:03:55 -04:00
parent 854614fc29
commit 1da1e810d2

View File

@ -39,9 +39,15 @@ use std::collections::HashMap;
let data = vec![(1, "hello"), (2, "world")];
let map: HashMap<i32, &'static str> = data.into_iter().collect();
let map: HashMap<_, _> = data.into_iter().collect();
```
The type annotation `HashMap<_, _>` is needed here because it's possible to
`collect` into many different data structures, so Rust doesn't know which we
want. For the type parameters for the key and value types, however, we can use
underscores and Rust can infer the types that the HashMap contains based on the
types of the data in our vector.
## Accessing Values in a HashMap
We can get a value out of the hashmap by providing the proper key to the `get`