|
| 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 | +} |
0 commit comments