mirror of
https://github.com/rust-lang-cn/book-cn.git
synced 2025-02-02 23:38:41 +08:00
I'd rather explain _ than 'static at this point tbh
This commit is contained in:
parent
854614fc29
commit
1da1e810d2
@ -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`
|
||||
|
Loading…
Reference in New Issue
Block a user