summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--rustlings/.rustlings-state.txt10
-rw-r--r--rustlings/exercises/18_iterators/iterators1.rs8
-rw-r--r--rustlings/exercises/18_iterators/iterators2.rs12
-rw-r--r--rustlings/exercises/18_iterators/iterators3.rs19
-rw-r--r--rustlings/exercises/18_iterators/iterators4.rs1
-rw-r--r--rustlings/exercises/18_iterators/iterators5.rs2
-rw-r--r--rustlings/solutions/18_iterators/iterators1.rs26
-rw-r--r--rustlings/solutions/18_iterators/iterators2.rs56
-rw-r--r--rustlings/solutions/18_iterators/iterators3.rs88
-rw-r--r--rustlings/solutions/18_iterators/iterators4.rs72
-rw-r--r--rustlings/solutions/18_iterators/iterators5.rs168
11 files changed, 438 insertions, 24 deletions
diff --git a/rustlings/.rustlings-state.txt b/rustlings/.rustlings-state.txt
index 4d58c9f..56f1a68 100644
--- a/rustlings/.rustlings-state.txt
+++ b/rustlings/.rustlings-state.txt
@@ -1,6 +1,6 @@
DON'T EDIT THIS FILE!
-tests3
+box1
intro1
intro2
@@ -70,4 +70,10 @@ lifetimes1
lifetimes2
lifetimes3
tests1
-tests2 \ No newline at end of file
+tests2
+tests3
+iterators1
+iterators2
+iterators3
+iterators4
+iterators5 \ No newline at end of file
diff --git a/rustlings/exercises/18_iterators/iterators1.rs b/rustlings/exercises/18_iterators/iterators1.rs
index ca937ed..71d8112 100644
--- a/rustlings/exercises/18_iterators/iterators1.rs
+++ b/rustlings/exercises/18_iterators/iterators1.rs
@@ -13,13 +13,13 @@ mod tests {
let my_fav_fruits = ["banana", "custard apple", "avocado", "peach", "raspberry"];
// TODO: Create an iterator over the array.
- let mut fav_fruits_iterator = todo!();
+ let mut fav_fruits_iterator = my_fav_fruits.iter();
assert_eq!(fav_fruits_iterator.next(), Some(&"banana"));
- assert_eq!(fav_fruits_iterator.next(), todo!()); // TODO: Replace `todo!()`
+ assert_eq!(fav_fruits_iterator.next(), Some(&"custard apple")); // TODO: Replace `todo!()`
assert_eq!(fav_fruits_iterator.next(), Some(&"avocado"));
- assert_eq!(fav_fruits_iterator.next(), todo!()); // TODO: Replace `todo!()`
+ assert_eq!(fav_fruits_iterator.next(), Some(&"peach")); // TODO: Replace `todo!()`
assert_eq!(fav_fruits_iterator.next(), Some(&"raspberry"));
- assert_eq!(fav_fruits_iterator.next(), todo!()); // TODO: Replace `todo!()`
+ assert_eq!(fav_fruits_iterator.next(), None); // TODO: Replace `todo!()`
}
}
diff --git a/rustlings/exercises/18_iterators/iterators2.rs b/rustlings/exercises/18_iterators/iterators2.rs
index 5903e65..c827529 100644
--- a/rustlings/exercises/18_iterators/iterators2.rs
+++ b/rustlings/exercises/18_iterators/iterators2.rs
@@ -7,7 +7,7 @@ fn capitalize_first(input: &str) -> String {
let mut chars = input.chars();
match chars.next() {
None => String::new(),
- Some(first) => todo!(),
+ Some(first) => first.to_uppercase().to_string() + &input[1..],
}
}
@@ -15,14 +15,20 @@ fn capitalize_first(input: &str) -> String {
// Return a vector of strings.
// ["hello", "world"] -> ["Hello", "World"]
fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
- // ???
+ let mut v = Vec::new();
+ for word in words {
+ if !word.trim().is_empty() {
+ v.push(capitalize_first(word));
+ }
+ }
+ v
}
// TODO: Apply the `capitalize_first` function again to a slice of string
// slices. Return a single string.
// ["hello", " ", "world"] -> "Hello World"
fn capitalize_words_string(words: &[&str]) -> String {
- // ???
+ capitalize_words_vector(words).join(" ")
}
fn main() {
diff --git a/rustlings/exercises/18_iterators/iterators3.rs b/rustlings/exercises/18_iterators/iterators3.rs
index dce0905..919739b 100644
--- a/rustlings/exercises/18_iterators/iterators3.rs
+++ b/rustlings/exercises/18_iterators/iterators3.rs
@@ -11,21 +11,30 @@ enum DivisionError {
// TODO: Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
// Otherwise, return a suitable error.
fn divide(a: i64, b: i64) -> Result<i64, DivisionError> {
- todo!();
+ if b == 0 {
+ return Err(DivisionError::DivideByZero);
+ }
+ if a == i64::MIN && b == -1 {
+ return Err(DivisionError::IntegerOverflow);
+ }
+ if a % b != 0 {
+ return Err(DivisionError::NotDivisible);
+ }
+ Ok(a / b)
}
// TODO: Add the correct return type and complete the function body.
// Desired output: `Ok([1, 11, 1426, 3])`
-fn result_with_list() {
+fn result_with_list() -> Result<Vec<i64>, DivisionError> {
let numbers = [27, 297, 38502, 81];
- let division_results = numbers.into_iter().map(|n| divide(n, 27));
+ numbers.into_iter().map(|n| divide(n, 27)).collect()
}
// TODO: Add the correct return type and complete the function body.
// Desired output: `[Ok(1), Ok(11), Ok(1426), Ok(3)]`
-fn list_of_results() {
+fn list_of_results() -> Vec<Result<i64, DivisionError>> {
let numbers = [27, 297, 38502, 81];
- let division_results = numbers.into_iter().map(|n| divide(n, 27));
+ numbers.into_iter().map(|n| divide(n, 27)).collect()
}
fn main() {
diff --git a/rustlings/exercises/18_iterators/iterators4.rs b/rustlings/exercises/18_iterators/iterators4.rs
index c296f0e..f153786 100644
--- a/rustlings/exercises/18_iterators/iterators4.rs
+++ b/rustlings/exercises/18_iterators/iterators4.rs
@@ -10,6 +10,7 @@ fn factorial(num: u64) -> u64 {
// - additional variables
// For an extra challenge, don't use:
// - recursion
+ (1..=num).reduce(|a, b| a * b).unwrap_or(1)
}
fn main() {
diff --git a/rustlings/exercises/18_iterators/iterators5.rs b/rustlings/exercises/18_iterators/iterators5.rs
index 7e434cc..74f1c74 100644
--- a/rustlings/exercises/18_iterators/iterators5.rs
+++ b/rustlings/exercises/18_iterators/iterators5.rs
@@ -26,6 +26,7 @@ fn count_for(map: &HashMap<String, Progress>, value: Progress) -> usize {
// TODO: Implement the functionality of `count_for` but with an iterator instead
// of a `for` loop.
fn count_iterator(map: &HashMap<String, Progress>, value: Progress) -> usize {
+ map.values().filter(|&&x| x == value).count()
// `map` is a hash map with `String` keys and `Progress` values.
// map = { "variables1": Complete, "from_str": None, … }
}
@@ -45,6 +46,7 @@ fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progres
// TODO: Implement the functionality of `count_collection_for` but with an
// iterator instead of a `for` loop.
fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
+ collection.iter().map(|m| count_iterator(m, value)).sum()
// `collection` is a slice of hash maps.
// collection = [{ "variables1": Complete, "from_str": None, … },
// { "variables2": Complete, … }, … ]
diff --git a/rustlings/solutions/18_iterators/iterators1.rs b/rustlings/solutions/18_iterators/iterators1.rs
index dcf2377..93a6008 100644
--- a/rustlings/solutions/18_iterators/iterators1.rs
+++ b/rustlings/solutions/18_iterators/iterators1.rs
@@ -1,4 +1,26 @@
+// When performing operations on elements within a collection, iterators are
+// essential. This module helps you get familiar with the structure of using an
+// iterator and how to go through elements within an iterable collection.
+
fn main() {
- // DON'T EDIT THIS SOLUTION FILE!
- // It will be automatically filled after you finish the exercise.
+ // You can optionally experiment here.
+}
+
+#[cfg(test)]
+mod tests {
+ #[test]
+ fn iterators() {
+ let my_fav_fruits = ["banana", "custard apple", "avocado", "peach", "raspberry"];
+
+ // Create an iterator over the array.
+ let mut fav_fruits_iterator = my_fav_fruits.iter();
+
+ assert_eq!(fav_fruits_iterator.next(), Some(&"banana"));
+ assert_eq!(fav_fruits_iterator.next(), Some(&"custard apple"));
+ assert_eq!(fav_fruits_iterator.next(), Some(&"avocado"));
+ assert_eq!(fav_fruits_iterator.next(), Some(&"peach"));
+ assert_eq!(fav_fruits_iterator.next(), Some(&"raspberry"));
+ assert_eq!(fav_fruits_iterator.next(), None);
+ // ^^^^ reached the end
+ }
}
diff --git a/rustlings/solutions/18_iterators/iterators2.rs b/rustlings/solutions/18_iterators/iterators2.rs
index dcf2377..db05f29 100644
--- a/rustlings/solutions/18_iterators/iterators2.rs
+++ b/rustlings/solutions/18_iterators/iterators2.rs
@@ -1,4 +1,56 @@
+// In this exercise, you'll learn some of the unique advantages that iterators
+// can offer.
+
+// "hello" -> "Hello"
+fn capitalize_first(input: &str) -> String {
+ let mut chars = input.chars();
+ match chars.next() {
+ None => String::new(),
+ Some(first) => first.to_uppercase().to_string() + chars.as_str(),
+ }
+}
+
+// Apply the `capitalize_first` function to a slice of string slices.
+// Return a vector of strings.
+// ["hello", "world"] -> ["Hello", "World"]
+fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
+ words.iter().map(|word| capitalize_first(word)).collect()
+}
+
+// Apply the `capitalize_first` function again to a slice of string
+// slices. Return a single string.
+// ["hello", " ", "world"] -> "Hello World"
+fn capitalize_words_string(words: &[&str]) -> String {
+ words.iter().map(|word| capitalize_first(word)).collect()
+}
+
fn main() {
- // DON'T EDIT THIS SOLUTION FILE!
- // It will be automatically filled after you finish the exercise.
+ // You can optionally experiment here.
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_success() {
+ assert_eq!(capitalize_first("hello"), "Hello");
+ }
+
+ #[test]
+ fn test_empty() {
+ assert_eq!(capitalize_first(""), "");
+ }
+
+ #[test]
+ fn test_iterate_string_vec() {
+ let words = vec!["hello", "world"];
+ assert_eq!(capitalize_words_vector(&words), ["Hello", "World"]);
+ }
+
+ #[test]
+ fn test_iterate_into_string() {
+ let words = vec!["hello", " ", "world"];
+ assert_eq!(capitalize_words_string(&words), "Hello World");
+ }
}
diff --git a/rustlings/solutions/18_iterators/iterators3.rs b/rustlings/solutions/18_iterators/iterators3.rs
index dcf2377..1d5d67f 100644
--- a/rustlings/solutions/18_iterators/iterators3.rs
+++ b/rustlings/solutions/18_iterators/iterators3.rs
@@ -1,4 +1,88 @@
+#[derive(Debug, PartialEq, Eq)]
+enum DivisionError {
+ // Example: 42 / 0
+ DivideByZero,
+ // Only case for `i64`: `i64::MIN / -1` because the result is `i64::MAX + 1`
+ IntegerOverflow,
+ // Example: 5 / 2 = 2.5
+ NotDivisible,
+}
+
+fn divide(a: i64, b: i64) -> Result<i64, DivisionError> {
+ if b == 0 {
+ return Err(DivisionError::DivideByZero);
+ }
+
+ if a == i64::MIN && b == -1 {
+ return Err(DivisionError::IntegerOverflow);
+ }
+
+ if a % b != 0 {
+ return Err(DivisionError::NotDivisible);
+ }
+
+ Ok(a / b)
+}
+
+fn result_with_list() -> Result<Vec<i64>, DivisionError> {
+ // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+ let numbers = [27, 297, 38502, 81];
+ let division_results = numbers.into_iter().map(|n| divide(n, 27));
+ // Collects to the expected return type. Returns the first error in the
+ // division results (if one exists).
+ division_results.collect()
+}
+
+fn list_of_results() -> Vec<Result<i64, DivisionError>> {
+ // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+ let numbers = [27, 297, 38502, 81];
+ let division_results = numbers.into_iter().map(|n| divide(n, 27));
+ // Collects to the expected return type.
+ division_results.collect()
+}
+
fn main() {
- // DON'T EDIT THIS SOLUTION FILE!
- // It will be automatically filled after you finish the exercise.
+ // You can optionally experiment here.
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_success() {
+ assert_eq!(divide(81, 9), Ok(9));
+ assert_eq!(divide(81, -1), Ok(-81));
+ assert_eq!(divide(i64::MIN, i64::MIN), Ok(1));
+ }
+
+ #[test]
+ fn test_divide_by_0() {
+ assert_eq!(divide(81, 0), Err(DivisionError::DivideByZero));
+ }
+
+ #[test]
+ fn test_integer_overflow() {
+ assert_eq!(divide(i64::MIN, -1), Err(DivisionError::IntegerOverflow));
+ }
+
+ #[test]
+ fn test_not_divisible() {
+ assert_eq!(divide(81, 6), Err(DivisionError::NotDivisible));
+ }
+
+ #[test]
+ fn test_divide_0_by_something() {
+ assert_eq!(divide(0, 81), Ok(0));
+ }
+
+ #[test]
+ fn test_result_with_list() {
+ assert_eq!(result_with_list().unwrap(), [1, 11, 1426, 3]);
+ }
+
+ #[test]
+ fn test_list_of_results() {
+ assert_eq!(list_of_results(), [Ok(1), Ok(11), Ok(1426), Ok(3)]);
+ }
}
diff --git a/rustlings/solutions/18_iterators/iterators4.rs b/rustlings/solutions/18_iterators/iterators4.rs
index dcf2377..4168835 100644
--- a/rustlings/solutions/18_iterators/iterators4.rs
+++ b/rustlings/solutions/18_iterators/iterators4.rs
@@ -1,4 +1,72 @@
+// 3 possible solutions are presented.
+
+// With `for` loop and a mutable variable.
+fn factorial_for(num: u64) -> u64 {
+ let mut result = 1;
+
+ for x in 2..=num {
+ result *= x;
+ }
+
+ result
+}
+
+// Equivalent to `factorial_for` but shorter and without a `for` loop and
+// mutable variables.
+fn factorial_fold(num: u64) -> u64 {
+ // Case num==0: The iterator 2..=0 is empty
+ // -> The initial value of `fold` is returned which is 1.
+ // Case num==1: The iterator 2..=1 is also empty
+ // -> The initial value 1 is returned.
+ // Case num==2: The iterator 2..=2 contains one element
+ // -> The initial value 1 is multiplied by 2 and the result
+ // is returned.
+ // Case num==3: The iterator 2..=3 contains 2 elements
+ // -> 1 * 2 is calculated, then the result 2 is multiplied by
+ // the second element 3 so the result 6 is returned.
+ // And so on…
+ #[allow(clippy::unnecessary_fold)]
+ (2..=num).fold(1, |acc, x| acc * x)
+}
+
+// Equivalent to `factorial_fold` but with a built-in method that is suggested
+// by Clippy.
+fn factorial_product(num: u64) -> u64 {
+ (2..=num).product()
+}
+
fn main() {
- // DON'T EDIT THIS SOLUTION FILE!
- // It will be automatically filled after you finish the exercise.
+ // You can optionally experiment here.
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn factorial_of_0() {
+ assert_eq!(factorial_for(0), 1);
+ assert_eq!(factorial_fold(0), 1);
+ assert_eq!(factorial_product(0), 1);
+ }
+
+ #[test]
+ fn factorial_of_1() {
+ assert_eq!(factorial_for(1), 1);
+ assert_eq!(factorial_fold(1), 1);
+ assert_eq!(factorial_product(1), 1);
+ }
+ #[test]
+ fn factorial_of_2() {
+ assert_eq!(factorial_for(2), 2);
+ assert_eq!(factorial_fold(2), 2);
+ assert_eq!(factorial_product(2), 2);
+ }
+
+ #[test]
+ fn factorial_of_4() {
+ assert_eq!(factorial_for(4), 24);
+ assert_eq!(factorial_fold(4), 24);
+ assert_eq!(factorial_product(4), 24);
+ }
}
diff --git a/rustlings/solutions/18_iterators/iterators5.rs b/rustlings/solutions/18_iterators/iterators5.rs
index dcf2377..067a117 100644
--- a/rustlings/solutions/18_iterators/iterators5.rs
+++ b/rustlings/solutions/18_iterators/iterators5.rs
@@ -1,4 +1,168 @@
+// Let's define a simple model to track Rustlings' exercise progress. Progress
+// will be modelled using a hash map. The name of the exercise is the key and
+// the progress is the value. Two counting functions were created to count the
+// number of exercises with a given progress. Recreate this counting
+// functionality using iterators. Try to not use imperative loops (for/while).
+
+use std::collections::HashMap;
+
+#[derive(Clone, Copy, PartialEq, Eq)]
+enum Progress {
+ None,
+ Some,
+ Complete,
+}
+
+fn count_for(map: &HashMap<String, Progress>, value: Progress) -> usize {
+ let mut count = 0;
+ for val in map.values() {
+ if *val == value {
+ count += 1;
+ }
+ }
+ count
+}
+
+fn count_iterator(map: &HashMap<String, Progress>, value: Progress) -> usize {
+ // `map` is a hash map with `String` keys and `Progress` values.
+ // map = { "variables1": Complete, "from_str": None, … }
+ map.values().filter(|val| **val == value).count()
+}
+
+fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
+ let mut count = 0;
+ for map in collection {
+ count += count_for(map, value);
+ }
+ count
+}
+
+fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
+ // `collection` is a slice of hash maps.
+ // collection = [{ "variables1": Complete, "from_str": None, … },
+ // { "variables2": Complete, … }, … ]
+ collection
+ .iter()
+ .map(|map| count_iterator(map, value))
+ .sum()
+}
+
+// Equivalent to `count_collection_iterator` and `count_iterator`, iterating as
+// if the collection was a single container instead of a container of containers
+// (and more accurately, a single iterator instead of an iterator of iterators).
+fn count_collection_iterator_flat(
+ collection: &[HashMap<String, Progress>],
+ value: Progress,
+) -> usize {
+ // `collection` is a slice of hash maps.
+ // collection = [{ "variables1": Complete, "from_str": None, … },
+ // { "variables2": Complete, … }, … ]
+ collection
+ .iter()
+ .flat_map(HashMap::values) // or just `.flatten()` when wanting the default iterator (`HashMap::iter`)
+ .filter(|val| **val == value)
+ .count()
+}
+
fn main() {
- // DON'T EDIT THIS SOLUTION FILE!
- // It will be automatically filled after you finish the exercise.
+ // You can optionally experiment here.
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use Progress::*;
+
+ fn get_map() -> HashMap<String, Progress> {
+ let mut map = HashMap::new();
+ map.insert(String::from("variables1"), Complete);
+ map.insert(String::from("functions1"), Complete);
+ map.insert(String::from("hashmap1"), Complete);
+ map.insert(String::from("arc1"), Some);
+ map.insert(String::from("as_ref_mut"), None);
+ map.insert(String::from("from_str"), None);
+
+ map
+ }
+
+ fn get_vec_map() -> Vec<HashMap<String, Progress>> {
+ let map = get_map();
+
+ let mut other = HashMap::new();
+ other.insert(String::from("variables2"), Complete);
+ other.insert(String::from("functions2"), Complete);
+ other.insert(String::from("if1"), Complete);
+ other.insert(String::from("from_into"), None);
+ other.insert(String::from("try_from_into"), None);
+
+ vec![map, other]
+ }
+
+ #[test]
+ fn count_complete() {
+ let map = get_map();
+ assert_eq!(count_iterator(&map, Complete), 3);
+ }
+
+ #[test]
+ fn count_some() {
+ let map = get_map();
+ assert_eq!(count_iterator(&map, Some), 1);
+ }
+
+ #[test]
+ fn count_none() {
+ let map = get_map();
+ assert_eq!(count_iterator(&map, None), 2);
+ }
+
+ #[test]
+ fn count_complete_equals_for() {
+ let map = get_map();
+ let progress_states = [Complete, Some, None];
+ for progress_state in progress_states {
+ assert_eq!(
+ count_for(&map, progress_state),
+ count_iterator(&map, progress_state),
+ );
+ }
+ }
+
+ #[test]
+ fn count_collection_complete() {
+ let collection = get_vec_map();
+ assert_eq!(count_collection_iterator(&collection, Complete), 6);
+ assert_eq!(count_collection_iterator_flat(&collection, Complete), 6);
+ }
+
+ #[test]
+ fn count_collection_some() {
+ let collection = get_vec_map();
+ assert_eq!(count_collection_iterator(&collection, Some), 1);
+ assert_eq!(count_collection_iterator_flat(&collection, Some), 1);
+ }
+
+ #[test]
+ fn count_collection_none() {
+ let collection = get_vec_map();
+ assert_eq!(count_collection_iterator(&collection, None), 4);
+ assert_eq!(count_collection_iterator_flat(&collection, None), 4);
+ }
+
+ #[test]
+ fn count_collection_equals_for() {
+ let collection = get_vec_map();
+ let progress_states = [Complete, Some, None];
+
+ for progress_state in progress_states {
+ assert_eq!(
+ count_collection_for(&collection, progress_state),
+ count_collection_iterator(&collection, progress_state),
+ );
+ assert_eq!(
+ count_collection_for(&collection, progress_state),
+ count_collection_iterator_flat(&collection, progress_state),
+ );
+ }
+ }
}