File tree Expand file tree Collapse file tree 2 files changed +4
-12
lines changed Expand file tree Collapse file tree 2 files changed +4
-12
lines changed Original file line number Diff line number Diff line change @@ -575,12 +575,8 @@ https://doc.rust-lang.org/book/ch08-03-hash-maps.html#only-inserting-a-value-if-
575
575
name = " hashmaps3"
576
576
dir = " 11_hashmaps"
577
577
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.
584
580
585
581
Hint 2: If there is already an entry for a given key, the value returned by
586
582
`entry()` can be updated based on the existing value.
Original file line number Diff line number Diff line change @@ -28,17 +28,13 @@ fn build_scores_table(results: &str) -> HashMap<&str, TeamScores> {
28
28
let team_2_score: u8 = split_iterator. next ( ) . unwrap ( ) . parse ( ) . unwrap ( ) ;
29
29
30
30
// 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 ( ) ;
34
32
// Update the values.
35
33
team_1. goals_scored += team_1_score;
36
34
team_1. goals_conceded += team_2_score;
37
35
38
36
// 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 ( ) ;
42
38
team_2. goals_scored += team_2_score;
43
39
team_2. goals_conceded += team_1_score;
44
40
}
You can’t perform that action at this time.
0 commit comments