Skip to content

Commit 79cc657

Browse files
authored
Merge pull request #646 from apogeeoak/iterator
Added iterators5.rs exercise.
2 parents 2b766ef + 9c88ea9 commit 79cc657

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// iterators5.rs
2+
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.
12+
//
13+
// Make the code compile and the tests pass.
14+
15+
// I AM NOT DONE
16+
17+
use std::collections::HashMap;
18+
19+
#[derive(PartialEq, Eq)]
20+
enum Progress {
21+
None,
22+
Some,
23+
Complete,
24+
}
25+
26+
fn count_for(map: &HashMap<String, Progress>, value: Progress) -> usize {
27+
let mut count = 0;
28+
for val in map.values() {
29+
if val == &value {
30+
count += 1;
31+
}
32+
}
33+
count
34+
}
35+
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, ... }
39+
}
40+
41+
fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
42+
let mut count = 0;
43+
for map in collection {
44+
for val in map.values() {
45+
if val == &value {
46+
count += 1;
47+
}
48+
}
49+
}
50+
count
51+
}
52+
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, ... }, ... ]
57+
}
58+
59+
#[cfg(test)]
60+
mod tests {
61+
use super::*;
62+
63+
#[test]
64+
fn count_complete() {
65+
let map = get_map();
66+
assert_eq!(3, count_iterator(&map, Progress::Complete));
67+
}
68+
69+
#[test]
70+
fn count_equals_for() {
71+
let map = get_map();
72+
assert_eq!(
73+
count_for(&map, Progress::Complete),
74+
count_iterator(&map, Progress::Complete)
75+
);
76+
}
77+
78+
#[test]
79+
fn count_collection_complete() {
80+
let collection = get_vec_map();
81+
assert_eq!(
82+
6,
83+
count_collection_iterator(&collection, Progress::Complete)
84+
);
85+
}
86+
87+
#[test]
88+
fn count_collection_equals_for() {
89+
let collection = get_vec_map();
90+
assert_eq!(
91+
count_collection_for(&collection, Progress::Complete),
92+
count_collection_iterator(&collection, Progress::Complete)
93+
);
94+
}
95+
96+
fn get_map() -> HashMap<String, Progress> {
97+
use Progress::*;
98+
99+
let mut map = HashMap::new();
100+
map.insert(String::from("variables1"), Complete);
101+
map.insert(String::from("functions1"), Complete);
102+
map.insert(String::from("hashmap1"), Complete);
103+
map.insert(String::from("arc1"), Some);
104+
map.insert(String::from("as_ref_mut"), None);
105+
map.insert(String::from("from_str"), None);
106+
107+
map
108+
}
109+
110+
fn get_vec_map() -> Vec<HashMap<String, Progress>> {
111+
use Progress::*;
112+
113+
let map = get_map();
114+
115+
let mut other = HashMap::new();
116+
other.insert(String::from("variables2"), Complete);
117+
other.insert(String::from("functions2"), Complete);
118+
other.insert(String::from("if1"), Complete);
119+
other.insert(String::from("from_into"), None);
120+
other.insert(String::from("try_from_into"), None);
121+
122+
vec![map, other]
123+
}
124+
}

info.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,22 @@ a mutable variable. Or, you might write code utilizing recursion
744744
and a match clause. In Rust you can take another functional
745745
approach, computing the factorial elegantly with ranges and iterators."""
746746

747+
[[exercises]]
748+
name = "iterators5"
749+
path = "exercises/standard_library_types/iterators5.rs"
750+
mode = "test"
751+
hint = """
752+
The documentation for the std::iter::Iterator trait contains numerous methods
753+
that would be helpful here.
754+
755+
Return 0 from count_collection_iterator to make the code compile in order to
756+
test count_iterator.
757+
758+
The collection variable in count_collection_iterator is a slice of HashMaps. It
759+
needs to be converted into an iterator in order to use the iterator methods.
760+
761+
The fold method can be useful in the count_collection_iterator function."""
762+
747763
# THREADS
748764

749765
[[exercises]]

0 commit comments

Comments
 (0)