Skip to content

Commit da55ce5

Browse files
committed
finish hashmaps
1 parent c57d6a7 commit da55ce5

4 files changed

Lines changed: 21 additions & 12 deletions

File tree

exercises/hashmaps/hashmaps1.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@
1111
// Execute `rustlings hint hashmaps1` or use the `hint` watch subcommand for a
1212
// hint.
1313

14-
// I AM NOT DONE
15-
1614
use std::collections::HashMap;
1715

1816
fn fruit_basket() -> HashMap<String, u32> {
19-
let mut basket = // TODO: declare your hash map here.
17+
let mut basket: HashMap<String, u32> = HashMap::new();
2018

2119
// Two bananas are already given for you :)
2220
basket.insert(String::from("banana"), 2);
2321

2422
// TODO: Put more fruits in your basket here.
23+
basket.insert(String::from("apple"), 8);
24+
basket.insert(String::from("pear"), 9);
2525

2626
basket
2727
}

exercises/hashmaps/hashmaps2.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
// Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a
1515
// hint.
1616

17-
// I AM NOT DONE
18-
1917
use std::collections::HashMap;
2018

2119
#[derive(Hash, PartialEq, Eq)]
@@ -40,6 +38,7 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
4038
// TODO: Insert new fruits if they are not already present in the
4139
// basket. Note that you are not allowed to put any type of fruit that's
4240
// already present!
41+
basket.entry(fruit).or_insert(1);
4342
}
4443
}
4544

@@ -90,4 +89,4 @@ mod tests {
9089
assert_ne!(amount, &0);
9190
}
9291
}
93-
}
92+
}

exercises/hashmaps/hashmaps3.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
// Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a
1515
// hint.
1616

17-
// I AM NOT DONE
18-
1917
use std::collections::HashMap;
2018

2119
// A structure to store the goal details of a team.
@@ -39,6 +37,20 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
3937
// will be the number of goals conceded from team_2, and similarly
4038
// goals scored by team_2 will be the number of goals conceded by
4139
// team_1.
40+
let entry1 = scores.entry(team_1_name).or_insert(Team {
41+
goals_scored: 0,
42+
goals_conceded: 0,
43+
});
44+
entry1.goals_scored += team_1_score;
45+
entry1.goals_conceded += team_2_score;
46+
47+
// 针对 team2 的记录更新
48+
let entry2 = scores.entry(team_2_name).or_insert(Team {
49+
goals_scored: 0,
50+
goals_conceded: 0,
51+
});
52+
entry2.goals_scored += team_2_score;
53+
entry2.goals_conceded += team_1_score;
4254
}
4355
scores
4456
}

exercises/modules/modules2.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77
// Execute `rustlings hint modules2` or use the `hint` watch subcommand for a
88
// hint.
99

10-
// I AM NOT DONE
11-
1210
mod delicious_snacks {
1311
// TODO: Fix these use statements
14-
use self::fruits::PEAR as ???
15-
use self::veggies::CUCUMBER as ???
12+
pub use self::fruits::PEAR as fruit;
13+
pub use self::veggies::CUCUMBER as veggie;
1614

1715
mod fruits {
1816
pub const PEAR: &'static str = "Pear";

0 commit comments

Comments
 (0)