Please help to solve iterators5.rs #2038
-
Hi stuck on this exercises could someone please help to solve it |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments
-
For the function inside |
Beta Was this translation helpful? Give feedback.
-
Alternatively, use map
.values()
.filter(|v| *v == &value)
.count() Note that in collection
.iter()
.map(|map| count_iterator(map, value))
.sum() |
Beta Was this translation helpful? Give feedback.
-
The Rustlings' hint is to use But... thinking of using And I would point to |
Beta Was this translation helpful? Give feedback.
-
from the hint:
|
Beta Was this translation helpful? Give feedback.
-
For the fold approach you can think of something like that: fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
collection.iter().fold(0, |acc, x| acc + count_iterator(x, value))
} |
Beta Was this translation helpful? Give feedback.
-
Both using fold: fn count_iterator(map: &HashMap<String, Progress>, value: Progress) -> usize {
// map is a hashmap with String keys and Progress values.
// map = { "variables1": Complete, "from_str": None, ... }
map.iter().fold(0, |acc, cur| if cur.1 == &value { return acc + 1 } else { acc })
} fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
// collection is a slice of hashmaps.
// collection = [{ "variables1": Complete, "from_str": None, ... },
// { "variables2": Complete, ... }, ... ]
collection.iter().fold(0, |acc, map| acc + count_iterator(map, value))
} |
Beta Was this translation helpful? Give feedback.
Alternatively, use
filter()
:Note that in
count_collection_iterator()
you can callcount_iterator()
instead of re-implementing the functionality. Something like: