Guest User

rustlings/11_hashmaps/hashmap3.rs

a guest
Sep 2nd, 2025
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.03 KB | Source Code | 0 0
  1. // A list of scores (one per line) of a soccer match is given. Each line is of
  2. // the form "<team_1_name>,<team_2_name>,<team_1_goals>,<team_2_goals>"
  3. // Example: "England,France,4,2" (England scored 4 goals, France 2).
  4. //
  5. // You have to build a scores table containing the name of the team, the total
  6. // number of goals the team scored, and the total number of goals the team
  7. // conceded.
  8.  
  9. use std::collections::HashMap;
  10.  
  11. // A structure to store the goal details of a team.
  12. #[derive(Default)]
  13. struct TeamScores {
  14.     goals_scored: u8,
  15.     goals_conceded: u8,
  16. }
  17.  
  18. fn build_scores_table(results: &str) -> HashMap<&str, TeamScores> {
  19.     // The name of the team is the key and its associated struct is the value.
  20.     let mut scores = HashMap::<&str, TeamScores>::new();
  21.  
  22.     for line in results.lines() {
  23.         let mut split_iterator = line.split(',');
  24.         // NOTE: We use `unwrap` because we didn't deal with error handling yet.
  25.         let team_1_name = split_iterator.next().unwrap();
  26.         let team_2_name = split_iterator.next().unwrap();
  27.         let team_1_score: u8 = split_iterator.next().unwrap().parse().unwrap();
  28.         let team_2_score: u8 = split_iterator.next().unwrap().parse().unwrap();
  29.  
  30.         // TODO: Populate the scores table with the extracted details.
  31.         // Keep in mind that goals scored by team 1 will be the number of goals
  32.         // conceded by team 2. Similarly, goals scored by team 2 will be the
  33.         // number of goals conceded by team 1.
  34.  
  35.         /*
  36.         let muy = & does not work
  37.         */
  38.         let  teamscores_1 = &mut scores.entry(team_1_name).or_insert(
  39.             TeamScores{goals_scored:0,
  40.                 goals_conceded:0});
  41.  
  42.         teamscores_1.goals_scored+=team_1_score;
  43.         teamscores_1.goals_conceded+=team_2_score;
  44.         let  teamscores_2 = &mut scores.entry(team_2_name).or_insert(
  45.             TeamScores{goals_scored:0,
  46.                 goals_conceded:0});
  47.         teamscores_2.goals_scored+=team_2_score;
  48.         teamscores_2.goals_conceded+=team_1_score;
  49.     }
  50.  
  51.  
  52.     scores
  53. }
  54.  
  55. fn main() {
  56.     // You can optionally experiment here.
  57. }
  58.  
  59. #[cfg(test)]
  60. mod tests {
  61.     use super::*;
  62.  
  63.     const RESULTS: &str = "England,France,4,2
  64. France,Italy,3,1
  65. Poland,Spain,2,0
  66. Germany,England,2,1
  67. England,Spain,1,0";
  68.  
  69.     #[test]
  70.     fn build_scores() {
  71.         let scores = build_scores_table(RESULTS);
  72.  
  73.         assert!(["England", "France", "Germany", "Italy", "Poland", "Spain"]
  74.             .into_iter()
  75.             .all(|team_name| scores.contains_key(team_name)));
  76.     }
  77.  
  78.     #[test]
  79.     fn validate_team_score_1() {
  80.         let scores = build_scores_table(RESULTS);
  81.         let team = scores.get("England").unwrap();
  82.         assert_eq!(team.goals_scored, 6);
  83.         assert_eq!(team.goals_conceded, 4);
  84.     }
  85.  
  86.     #[test]
  87.     fn validate_team_score_2() {
  88.         let scores = build_scores_table(RESULTS);
  89.         let team = scores.get("Spain").unwrap();
  90.         assert_eq!(team.goals_scored, 0);
  91.         assert_eq!(team.goals_conceded, 3);
  92.     }
  93. }
  94.  
Tags: rust
Advertisement
Add Comment
Please, Sign In to add comment