mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-23 04:29:41 +00:00
update HashMap.md
This commit is contained in:
@ -0,0 +1,73 @@
|
||||
1.
|
||||
```rust
|
||||
// FILL in the blanks and FIX the erros
|
||||
use std::collections::HashMap;
|
||||
fn main() {
|
||||
let mut scores = HashMap::new();
|
||||
scores.insert("Sunface", 98);
|
||||
scores.insert("Daniel", 95);
|
||||
scores.insert("Ashley", 69);
|
||||
scores.insert("Katie", 58);
|
||||
|
||||
// get returns a Option<&V>
|
||||
let score = scores.get("Sunface");
|
||||
assert_eq!(score, Some(&98));
|
||||
|
||||
if scores.contains_key("Daniel") {
|
||||
// indexing return a value V
|
||||
let score = scores["Daniel"];
|
||||
assert_eq!(score, 95);
|
||||
scores.remove("Daniel");
|
||||
}
|
||||
|
||||
assert_eq!(scores.len(), 3);
|
||||
|
||||
for (name, score) in scores {
|
||||
println!("The score of {} is {}", name, score)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
2.
|
||||
```rust
|
||||
use std::collections::HashMap;
|
||||
fn main() {
|
||||
let teams = [
|
||||
("Chinese Team", 100),
|
||||
("American Team", 10),
|
||||
("France Team", 50),
|
||||
];
|
||||
|
||||
let mut teams_map1 = HashMap::new();
|
||||
for team in &teams {
|
||||
teams_map1.insert(team.0, team.1);
|
||||
}
|
||||
|
||||
let teams_map2: HashMap<_,_> = teams.into_iter().collect();
|
||||
|
||||
assert_eq!(teams_map1, teams_map2);
|
||||
|
||||
println!("Success!")
|
||||
}
|
||||
```
|
||||
|
||||
```rust
|
||||
use std::{collections::HashMap};
|
||||
fn main() {
|
||||
let teams = [
|
||||
("Chinese Team", 100),
|
||||
("American Team", 10),
|
||||
("France Team", 50),
|
||||
];
|
||||
|
||||
let mut teams_map1 = HashMap::new();
|
||||
for team in &teams {
|
||||
teams_map1.insert(team.0, team.1);
|
||||
}
|
||||
|
||||
let teams_map2 = HashMap::from(teams);
|
||||
assert_eq!(teams_map1, teams_map2);
|
||||
|
||||
println!("Success!")
|
||||
}
|
||||
```
|
Reference in New Issue
Block a user