mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-05 02:09:18 +00:00
Refactor factorial function using custom iterator
This commit is contained in:
parent
77c6ac4868
commit
eb99e60ed8
@ -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<String, Progress>, value: Progress) -> usize {
|
||||
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, ... }
|
||||
todo!();
|
||||
map.values().filter(|&val| val == &value).count()
|
||||
}
|
||||
|
||||
fn count_collection_for(collection: &[HashMap<String, Progress>], 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<String, Progress>], 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)]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user