From eb99e60ed8838ad152967885e01d8016760a251f Mon Sep 17 00:00:00 2001 From: Rock070 Date: Thu, 11 Jan 2024 23:22:52 +0800 Subject: [PATCH] Refactor factorial function using custom iterator --- exercises/18_iterators/iterators5.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/exercises/18_iterators/iterators5.rs b/exercises/18_iterators/iterators5.rs index a062ee4c..a57e9720 100644 --- a/exercises/18_iterators/iterators5.rs +++ b/exercises/18_iterators/iterators5.rs @@ -11,8 +11,6 @@ // Execute `rustlings hint iterators5` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE - use std::collections::HashMap; #[derive(Clone, Copy, PartialEq, Eq)] @@ -35,11 +33,11 @@ fn count_for(map: &HashMap, value: Progress) -> usize { fn count_iterator(map: &HashMap, value: Progress) -> usize { // map is a hashmap with String keys and Progress values. // map = { "variables1": Complete, "from_str": None, ... } - todo!(); + map.values().filter(|&val| val == &value).count() } fn count_collection_for(collection: &[HashMap], value: Progress) -> usize { - let mut count = 0; + let mut count: usize = 0; for map in collection { for val in map.values() { if val == &value { @@ -54,7 +52,12 @@ fn count_collection_iterator(collection: &[HashMap], value: Pr // collection is a slice of hashmaps. // collection = [{ "variables1": Complete, "from_str": None, ... }, // { "variables2": Complete, ... }, ... ] - todo!(); + let mut count = 0; + collection.iter().for_each(|f| { + count += count_iterator(f, value) + }); + + count } #[cfg(test)]