mirror of
https://github.com/notohh/rustlings.git
synced 2024-11-26 07:14:17 -05:00
Improved iterators5.rs explanation.
This commit is contained in:
parent
b29ea17ea9
commit
9c88ea9126
2 changed files with 37 additions and 25 deletions
|
@ -1,11 +1,14 @@
|
||||||
// iterators5.rs
|
// iterators5.rs
|
||||||
|
|
||||||
// Rustling progress is modelled using a hash map. The name of the exercise is
|
// Let's define a simple model to track Rustlings exercise progress. Progress
|
||||||
// the key and the progress is the value. Two counting functions were created
|
// will be modelled using a hash map. The name of the exercise is the key and
|
||||||
// to count the number of exercises with a given progress. These counting
|
// the progress is the value. Two counting functions were created to count the
|
||||||
// functions use imperative style for loops. Recreate this counting
|
// number of exercises with a given progress. These counting functions use
|
||||||
// functionality using iterators.
|
// imperative style for loops. Recreate this counting functionality using
|
||||||
// Execute `rustlings hint iterators5` for hints.
|
// iterators. Only the two iterator methods (count_iterator and
|
||||||
|
// count_collection_iterator) need to be modified.
|
||||||
|
// Execute `rustlings hint
|
||||||
|
// iterators5` for hints.
|
||||||
//
|
//
|
||||||
// Make the code compile and the tests pass.
|
// Make the code compile and the tests pass.
|
||||||
|
|
||||||
|
@ -30,12 +33,14 @@ fn count_for(map: &HashMap<String, Progress>, value: Progress) -> usize {
|
||||||
count
|
count
|
||||||
}
|
}
|
||||||
|
|
||||||
fn count(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, ... }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn count_stack_for(stack: &[HashMap<String, Progress>], value: Progress) -> usize {
|
fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
|
||||||
let mut count = 0;
|
let mut count = 0;
|
||||||
for map in stack {
|
for map in collection {
|
||||||
for val in map.values() {
|
for val in map.values() {
|
||||||
if val == &value {
|
if val == &value {
|
||||||
count += 1;
|
count += 1;
|
||||||
|
@ -45,7 +50,10 @@ fn count_stack_for(stack: &[HashMap<String, Progress>], value: Progress) -> usiz
|
||||||
count
|
count
|
||||||
}
|
}
|
||||||
|
|
||||||
fn count_stack(stack: &[HashMap<String, Progress>], value: Progress) -> usize {
|
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, ... }, ... ]
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -55,7 +63,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn count_complete() {
|
fn count_complete() {
|
||||||
let map = get_map();
|
let map = get_map();
|
||||||
assert_eq!(3, count(&map, Progress::Complete));
|
assert_eq!(3, count_iterator(&map, Progress::Complete));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -63,22 +71,25 @@ mod tests {
|
||||||
let map = get_map();
|
let map = get_map();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
count_for(&map, Progress::Complete),
|
count_for(&map, Progress::Complete),
|
||||||
count(&map, Progress::Complete)
|
count_iterator(&map, Progress::Complete)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn count_stack_complete() {
|
fn count_collection_complete() {
|
||||||
let stack = get_map_stack();
|
let collection = get_vec_map();
|
||||||
assert_eq!(6, count_stack(&stack, Progress::Complete));
|
assert_eq!(
|
||||||
|
6,
|
||||||
|
count_collection_iterator(&collection, Progress::Complete)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn count_stack_equals_for() {
|
fn count_collection_equals_for() {
|
||||||
let stack = get_map_stack();
|
let collection = get_vec_map();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
count_stack_for(&stack, Progress::Complete),
|
count_collection_for(&collection, Progress::Complete),
|
||||||
count_stack(&stack, Progress::Complete)
|
count_collection_iterator(&collection, Progress::Complete)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,7 +107,7 @@ mod tests {
|
||||||
map
|
map
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_map_stack() -> Vec<HashMap<String, Progress>> {
|
fn get_vec_map() -> Vec<HashMap<String, Progress>> {
|
||||||
use Progress::*;
|
use Progress::*;
|
||||||
|
|
||||||
let map = get_map();
|
let map = get_map();
|
||||||
|
|
|
@ -749,12 +749,13 @@ hint = """
|
||||||
The documentation for the std::iter::Iterator trait contains numerous methods
|
The documentation for the std::iter::Iterator trait contains numerous methods
|
||||||
that would be helpful here.
|
that would be helpful here.
|
||||||
|
|
||||||
Return 0 from count_stack to make the code compile in order to test count.
|
Return 0 from count_collection_iterator to make the code compile in order to
|
||||||
|
test count_iterator.
|
||||||
|
|
||||||
The stack variable in count_stack is a slice of HashMaps. It needs to be
|
The collection variable in count_collection_iterator is a slice of HashMaps. It
|
||||||
converted into an iterator in order to use the iterator methods.
|
needs to be converted into an iterator in order to use the iterator methods.
|
||||||
|
|
||||||
The fold method can be useful in the count_stack function."""
|
The fold method can be useful in the count_collection_iterator function."""
|
||||||
|
|
||||||
# THREADS
|
# THREADS
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue