Skip to content

Commit 9c88ea9

Browse files
committed
Improved iterators5.rs explanation.
1 parent b29ea17 commit 9c88ea9

File tree

2 files changed

+37
-25
lines changed

2 files changed

+37
-25
lines changed

exercises/standard_library_types/iterators5.rs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
// iterators5.rs
22

3-
// Rustling progress is modelled using a hash map. The name of the exercise is
4-
// the key and the progress is the value. Two counting functions were created
5-
// to count the number of exercises with a given progress. These counting
6-
// functions use imperative style for loops. Recreate this counting
7-
// functionality using iterators.
8-
// Execute `rustlings hint iterators5` for hints.
3+
// Let's define a simple model to track Rustlings exercise progress. Progress
4+
// will be modelled using a hash map. The name of the exercise is the key and
5+
// the progress is the value. Two counting functions were created to count the
6+
// number of exercises with a given progress. These counting functions use
7+
// imperative style for loops. Recreate this counting functionality using
8+
// iterators. Only the two iterator methods (count_iterator and
9+
// count_collection_iterator) need to be modified.
10+
// Execute `rustlings hint
11+
// iterators5` for hints.
912
//
1013
// Make the code compile and the tests pass.
1114

@@ -30,12 +33,14 @@ fn count_for(map: &HashMap<String, Progress>, value: Progress) -> usize {
3033
count
3134
}
3235

33-
fn count(map: &HashMap<String, Progress>, value: Progress) -> usize {
36+
fn count_iterator(map: &HashMap<String, Progress>, value: Progress) -> usize {
37+
// map is a hashmap with String keys and Progress values.
38+
// map = { "variables1": Complete, "from_str": None, ... }
3439
}
3540

36-
fn count_stack_for(stack: &[HashMap<String, Progress>], value: Progress) -> usize {
41+
fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
3742
let mut count = 0;
38-
for map in stack {
43+
for map in collection {
3944
for val in map.values() {
4045
if val == &value {
4146
count += 1;
@@ -45,7 +50,10 @@ fn count_stack_for(stack: &[HashMap<String, Progress>], value: Progress) -> usiz
4550
count
4651
}
4752

48-
fn count_stack(stack: &[HashMap<String, Progress>], value: Progress) -> usize {
53+
fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
54+
// collection is a slice of hashmaps.
55+
// collection = [{ "variables1": Complete, "from_str": None, ... },
56+
// { "variables2": Complete, ... }, ... ]
4957
}
5058

5159
#[cfg(test)]
@@ -55,30 +63,33 @@ mod tests {
5563
#[test]
5664
fn count_complete() {
5765
let map = get_map();
58-
assert_eq!(3, count(&map, Progress::Complete));
66+
assert_eq!(3, count_iterator(&map, Progress::Complete));
5967
}
6068

6169
#[test]
6270
fn count_equals_for() {
6371
let map = get_map();
6472
assert_eq!(
6573
count_for(&map, Progress::Complete),
66-
count(&map, Progress::Complete)
74+
count_iterator(&map, Progress::Complete)
6775
);
6876
}
6977

7078
#[test]
71-
fn count_stack_complete() {
72-
let stack = get_map_stack();
73-
assert_eq!(6, count_stack(&stack, Progress::Complete));
79+
fn count_collection_complete() {
80+
let collection = get_vec_map();
81+
assert_eq!(
82+
6,
83+
count_collection_iterator(&collection, Progress::Complete)
84+
);
7485
}
7586

7687
#[test]
77-
fn count_stack_equals_for() {
78-
let stack = get_map_stack();
88+
fn count_collection_equals_for() {
89+
let collection = get_vec_map();
7990
assert_eq!(
80-
count_stack_for(&stack, Progress::Complete),
81-
count_stack(&stack, Progress::Complete)
91+
count_collection_for(&collection, Progress::Complete),
92+
count_collection_iterator(&collection, Progress::Complete)
8293
);
8394
}
8495

@@ -96,7 +107,7 @@ mod tests {
96107
map
97108
}
98109

99-
fn get_map_stack() -> Vec<HashMap<String, Progress>> {
110+
fn get_vec_map() -> Vec<HashMap<String, Progress>> {
100111
use Progress::*;
101112

102113
let map = get_map();

info.toml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ Step 2 & step 2.1:
694694
Very similar to the lines above and below. You've got this!
695695
Step 3:
696696
An iterator goes through all elements in a collection, but what if we've run out of
697-
elements? What should we expect here? If you're stuck, take a look at
697+
elements? What should we expect here? If you're stuck, take a look at
698698
https://doc.rust-lang.org/std/iter/trait.Iterator.html for some ideas.
699699
"""
700700

@@ -749,12 +749,13 @@ hint = """
749749
The documentation for the std::iter::Iterator trait contains numerous methods
750750
that would be helpful here.
751751
752-
Return 0 from count_stack to make the code compile in order to test count.
752+
Return 0 from count_collection_iterator to make the code compile in order to
753+
test count_iterator.
753754
754-
The stack variable in count_stack is a slice of HashMaps. It needs to be
755-
converted into an iterator in order to use the iterator methods.
755+
The collection variable in count_collection_iterator is a slice of HashMaps. It
756+
needs to be converted into an iterator in order to use the iterator methods.
756757
757-
The fold method can be useful in the count_stack function."""
758+
The fold method can be useful in the count_collection_iterator function."""
758759

759760
# THREADS
760761

0 commit comments

Comments
 (0)