From 45641fa13a905048c354f852986b1ac011719fce Mon Sep 17 00:00:00 2001 From: Peter Tseng Date: Sun, 3 Jul 2016 03:48:27 -0700 Subject: [PATCH 1/4] tournament: Use `Entry#or_insert` in `add_game_result` This drastically shortens the function, avoiding the need to `match` on the `Entry` cases. This seems good to make the example more idiomatic. --- exercises/tournament/example.rs | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/exercises/tournament/example.rs b/exercises/tournament/example.rs index 8d741007c..13c793dc3 100644 --- a/exercises/tournament/example.rs +++ b/exercises/tournament/example.rs @@ -1,6 +1,5 @@ use std::cmp::Ordering::Equal; use std::collections::HashMap; -use std::collections::hash_map::Entry; use std::fs::File; use std::io::{BufRead, BufReader, Write, Result}; use std::path::Path; @@ -87,14 +86,5 @@ fn write_tally(results: &HashMap, output_filename: &Path) -> } fn add_game_result(results: &mut HashMap, team: String, result: GameResult) { - match results.entry(team) { - Entry::Vacant(entry) => { - let mut tr = TeamResult::new(); - tr.add_game_result(result); - entry.insert(tr); - } - Entry::Occupied(mut entry) => { - (*entry.get_mut()).add_game_result(result); - } - }; + results.entry(team).or_insert(TeamResult::new()).add_game_result(result); } From 1006f91f59382ac4cca264287cc69f52af5d3cfb Mon Sep 17 00:00:00 2001 From: Peter Tseng Date: Sun, 3 Jul 2016 03:57:00 -0700 Subject: [PATCH 2/4] parallel-letter-frequency: Use `Entry#or_insert` This drastically shortens the `count` function and result collection loop in `frequency`, avoiding the need to `match` on the `Entry` cases. This seems good to make the example more idiomatic. --- exercises/parallel-letter-frequency/example.rs | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/exercises/parallel-letter-frequency/example.rs b/exercises/parallel-letter-frequency/example.rs index d0dbdbeda..3897a89a8 100644 --- a/exercises/parallel-letter-frequency/example.rs +++ b/exercises/parallel-letter-frequency/example.rs @@ -1,5 +1,4 @@ use std::collections::HashMap; -use std::collections::hash_map::Entry; use std::thread; use std::sync::mpsc::channel; @@ -36,12 +35,7 @@ pub fn frequency(texts: &[&str], num_workers: usize) -> HashMap { for _ in 0..num_workers { let part_results = rx.recv().unwrap(); for (c, n) in part_results.into_iter() { - match results.entry(c) { - Entry::Vacant(view) => { view.insert(n); }, - Entry::Occupied(mut view) => { - *view.get_mut() += n; - } - } + *results.entry(c).or_insert(0) += n; } } results @@ -52,12 +46,7 @@ fn count(lines: Vec) -> HashMap { for line in lines.iter() { for c in line.chars() { if c.is_alphabetic() { - match results.entry(c.to_lowercase().next().unwrap()) { - Entry::Vacant(view) => { view.insert(1); }, - Entry::Occupied(mut view) => { - *view.get_mut() += 1; - } - } + *results.entry(c.to_lowercase().next().unwrap()).or_insert(0) += 1; } } } From 8ebe85d41d508b1db8c7a1b9f352ca57f837e844 Mon Sep 17 00:00:00 2001 From: Peter Tseng Date: Sun, 3 Jul 2016 04:01:32 -0700 Subject: [PATCH 3/4] word-count: Use `Entry#or_insert` in `word_count` This drastically shortens the function, avoiding the need to `match` on the `Entry` cases. This seems good to make the example more idiomatic. --- exercises/word-count/example.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/exercises/word-count/example.rs b/exercises/word-count/example.rs index 787c8fc3e..08aab597b 100644 --- a/exercises/word-count/example.rs +++ b/exercises/word-count/example.rs @@ -1,5 +1,4 @@ use std::collections::HashMap; -use std::collections::hash_map::Entry; fn lowercase(word: &str) -> String { let mut lower = String::with_capacity(word.len()); @@ -16,12 +15,7 @@ pub fn word_count(input: &str) -> HashMap { let lower = lowercase(input); let slice: &str = lower.as_ref(); for word in slice.split(|c: char| !c.is_alphanumeric()).filter(|s| !s.is_empty()) { - match map.entry(word.to_string()) { - Entry::Vacant(entry) => { entry.insert(1); }, - Entry::Occupied(mut entry) => { - *entry.get_mut() += 1; - } - }; + *map.entry(word.to_string()).or_insert(0) += 1; } map } From a36edc7991254434f6b5a895c4e441528f8fb62f Mon Sep 17 00:00:00 2001 From: Peter Tseng Date: Sun, 3 Jul 2016 05:08:46 -0700 Subject: [PATCH 4/4] grade-school: Use `Entry#or_insert` in `add` This drastically shortens the function, avoiding the need to `match` on the `Entry` cases. This seems good to make the example more idiomatic. --- exercises/grade-school/example.rs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/exercises/grade-school/example.rs b/exercises/grade-school/example.rs index 5e3811cc9..e0d6d1f0f 100644 --- a/exercises/grade-school/example.rs +++ b/exercises/grade-school/example.rs @@ -1,5 +1,4 @@ use std::collections::HashMap; -use std::collections::hash_map::Entry; pub struct School { grades: HashMap> @@ -11,14 +10,9 @@ impl School { } pub fn add(&mut self, grade: u32, student: &str) { - match self.grades.entry(grade) { - Entry::Vacant(view) => { view.insert(vec![student.to_string()]); } - Entry::Occupied(mut view) => { - let l = view.get_mut(); - l.push(student.to_string()); - l.sort(); - }, - }; + let mut entry = self.grades.entry(grade).or_insert(Vec::new()); + entry.push(student.to_string()); + entry.sort(); } pub fn grades(&self) -> Vec {