Skip to content

Commit f146553

Browse files
committed
hashmap3: Use or_default
1 parent 0432e07 commit f146553

File tree

2 files changed

+4
-12
lines changed

2 files changed

+4
-12
lines changed

rustlings-macros/info.toml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -575,12 +575,8 @@ https://doc.rust-lang.org/book/ch08-03-hash-maps.html#only-inserting-a-value-if-
575575
name = "hashmaps3"
576576
dir = "11_hashmaps"
577577
hint = """
578-
Hint 1: Use the `entry()` and `or_insert()` (or `or_insert_with()`) methods of
579-
`HashMap` to insert the default value of `TeamScores` if a team doesn't
580-
exist in the table yet.
581-
582-
Learn more in The Book:
583-
https://doc.rust-lang.org/book/ch08-03-hash-maps.html#only-inserting-a-value-if-the-key-has-no-value
578+
Hint 1: Use the `entry()` and `or_default()` methods of `HashMap` to insert the
579+
default value of `TeamScores` if a team doesn't exist in the table yet.
584580
585581
Hint 2: If there is already an entry for a given key, the value returned by
586582
`entry()` can be updated based on the existing value.

solutions/11_hashmaps/hashmaps3.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,13 @@ fn build_scores_table(results: &str) -> HashMap<&str, TeamScores> {
2828
let team_2_score: u8 = split_iterator.next().unwrap().parse().unwrap();
2929

3030
// Insert the default with zeros if a team doesn't exist yet.
31-
let team_1 = scores
32-
.entry(team_1_name)
33-
.or_insert_with(TeamScores::default);
31+
let team_1 = scores.entry(team_1_name).or_default();
3432
// Update the values.
3533
team_1.goals_scored += team_1_score;
3634
team_1.goals_conceded += team_2_score;
3735

3836
// Similarly for the second team.
39-
let team_2 = scores
40-
.entry(team_2_name)
41-
.or_insert_with(TeamScores::default);
37+
let team_2 = scores.entry(team_2_name).or_default();
4238
team_2.goals_scored += team_2_score;
4339
team_2.goals_conceded += team_1_score;
4440
}

0 commit comments

Comments
 (0)