1
1
// iterators5.rs
2
2
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.
9
12
//
10
13
// Make the code compile and the tests pass.
11
14
@@ -30,12 +33,14 @@ fn count_for(map: &HashMap<String, Progress>, value: Progress) -> usize {
30
33
count
31
34
}
32
35
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, ... }
34
39
}
35
40
36
- fn count_stack_for ( stack : & [ HashMap < String , Progress > ] , value : Progress ) -> usize {
41
+ fn count_collection_for ( collection : & [ HashMap < String , Progress > ] , value : Progress ) -> usize {
37
42
let mut count = 0 ;
38
- for map in stack {
43
+ for map in collection {
39
44
for val in map. values ( ) {
40
45
if val == & value {
41
46
count += 1 ;
@@ -45,7 +50,10 @@ fn count_stack_for(stack: &[HashMap<String, Progress>], value: Progress) -> usiz
45
50
count
46
51
}
47
52
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, ... }, ... ]
49
57
}
50
58
51
59
#[ cfg( test) ]
@@ -55,30 +63,33 @@ mod tests {
55
63
#[ test]
56
64
fn count_complete ( ) {
57
65
let map = get_map ( ) ;
58
- assert_eq ! ( 3 , count ( & map, Progress :: Complete ) ) ;
66
+ assert_eq ! ( 3 , count_iterator ( & map, Progress :: Complete ) ) ;
59
67
}
60
68
61
69
#[ test]
62
70
fn count_equals_for ( ) {
63
71
let map = get_map ( ) ;
64
72
assert_eq ! (
65
73
count_for( & map, Progress :: Complete ) ,
66
- count ( & map, Progress :: Complete )
74
+ count_iterator ( & map, Progress :: Complete )
67
75
) ;
68
76
}
69
77
70
78
#[ 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
+ ) ;
74
85
}
75
86
76
87
#[ 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 ( ) ;
79
90
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 )
82
93
) ;
83
94
}
84
95
@@ -96,7 +107,7 @@ mod tests {
96
107
map
97
108
}
98
109
99
- fn get_map_stack ( ) -> Vec < HashMap < String , Progress > > {
110
+ fn get_vec_map ( ) -> Vec < HashMap < String , Progress > > {
100
111
use Progress :: * ;
101
112
102
113
let map = get_map ( ) ;
0 commit comments