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 ( ) ;
0 commit comments