summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo8it <mo8it@proton.me>2024-06-28 15:00:13 +0200
committermo8it <mo8it@proton.me>2024-06-28 15:00:13 +0200
commit56a9197f55356a0a6503d6fa6cb2241d676bd051 (patch)
tree9515db708c2646e2eaf7f53b112dbe16b8fff7de
parenteddbb97934b8d358b4fd20cc3063cf4872e39567 (diff)
iterators3 solution
-rw-r--r--exercises/18_iterators/iterators3.rs55
-rw-r--r--rustlings-macros/info.toml6
-rw-r--r--solutions/18_iterators/iterators3.rs74
3 files changed, 92 insertions, 43 deletions
diff --git a/exercises/18_iterators/iterators3.rs b/exercises/18_iterators/iterators3.rs
index dfe4014..b5d05f6 100644
--- a/exercises/18_iterators/iterators3.rs
+++ b/exercises/18_iterators/iterators3.rs
@@ -1,40 +1,26 @@
-// This is a bigger exercise than most of the others! You can do it! Here is
-// your mission, should you choose to accept it:
-// 1. Complete the divide function to get the first four tests to pass.
-// 2. Get the remaining tests to pass by completing the result_with_list and
-// list_of_results functions.
-
#[derive(Debug, PartialEq, Eq)]
enum DivisionError {
- NotDivisible(NotDivisibleError),
DivideByZero,
+ NotDivisible,
}
-#[derive(Debug, PartialEq, Eq)]
-struct NotDivisibleError {
- dividend: i32,
- divisor: i32,
-}
-
-// Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
+// TODO: Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
// Otherwise, return a suitable error.
fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {
todo!();
}
-// Complete the function and return a value of the correct type so the test
-// passes.
-// Desired output: Ok([1, 11, 1426, 3])
-fn result_with_list() -> () {
- let numbers = vec![27, 297, 38502, 81];
+// TODO: Add the correct return type and complete the function body.
+// Desired output: `Ok([1, 11, 1426, 3])`
+fn result_with_list() {
+ let numbers = [27, 297, 38502, 81];
let division_results = numbers.into_iter().map(|n| divide(n, 27));
}
-// Complete the function and return a value of the correct type so the test
-// passes.
-// Desired output: [Ok(1), Ok(11), Ok(1426), Ok(3)]
-fn list_of_results() -> () {
- let numbers = vec![27, 297, 38502, 81];
+// 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() {
+ let numbers = [27, 297, 38502, 81];
let division_results = numbers.into_iter().map(|n| divide(n, 27));
}
@@ -52,19 +38,13 @@ mod tests {
}
#[test]
- fn test_not_divisible() {
- assert_eq!(
- divide(81, 6),
- Err(DivisionError::NotDivisible(NotDivisibleError {
- dividend: 81,
- divisor: 6
- }))
- );
+ fn test_divide_by_0() {
+ assert_eq!(divide(81, 0), Err(DivisionError::DivideByZero));
}
#[test]
- fn test_divide_by_0() {
- assert_eq!(divide(81, 0), Err(DivisionError::DivideByZero));
+ fn test_not_divisible() {
+ assert_eq!(divide(81, 6), Err(DivisionError::NotDivisible));
}
#[test]
@@ -74,14 +54,11 @@ mod tests {
#[test]
fn test_result_with_list() {
- assert_eq!(format!("{:?}", result_with_list()), "Ok([1, 11, 1426, 3])");
+ assert_eq!(result_with_list().unwarp(), [1, 11, 1426, 3]);
}
#[test]
fn test_list_of_results() {
- assert_eq!(
- format!("{:?}", list_of_results()),
- "[Ok(1), Ok(11), Ok(1426), Ok(3)]"
- );
+ assert_eq!(list_of_results(), [Ok(1), Ok(11), Ok(1426), Ok(3)]);
}
}
diff --git a/rustlings-macros/info.toml b/rustlings-macros/info.toml
index 5a33788..8b1feb4 100644
--- a/rustlings-macros/info.toml
+++ b/rustlings-macros/info.toml
@@ -922,8 +922,8 @@ powerful and very general. Rust just needs to know the desired type."""
name = "iterators3"
dir = "18_iterators"
hint = """
-The `divide` function needs to return the correct error when even division is
-not possible.
+The `divide` function needs to return the correct error when the divisor is 0 or
+when even division is not possible.
The `division_results` variable needs to be collected into a collection type.
@@ -934,7 +934,7 @@ The `list_of_results` function needs to return a vector of results.
See https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.collect for
how the `FromIterator` trait is used in `collect()`. This trait is REALLY
-powerful! It can make the solution to this exercise infinitely easier."""
+powerful! It can make the solution to this exercise much easier."""
[[exercises]]
name = "iterators4"
diff --git a/solutions/18_iterators/iterators3.rs b/solutions/18_iterators/iterators3.rs
index 4e18198..d66d1ef 100644
--- a/solutions/18_iterators/iterators3.rs
+++ b/solutions/18_iterators/iterators3.rs
@@ -1 +1,73 @@
-// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
+#[derive(Debug, PartialEq, Eq)]
+enum DivisionError {
+ DivideByZero,
+ NotDivisible,
+}
+
+fn divide(a: i64, b: i64) -> Result<i64, DivisionError> {
+ if b == 0 {
+ return Err(DivisionError::DivideByZero);
+ }
+
+ 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() {
+ // You can optionally experiment here.
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_success() {
+ assert_eq!(divide(81, 9), Ok(9));
+ }
+
+ #[test]
+ fn test_divide_by_0() {
+ assert_eq!(divide(81, 0), Err(DivisionError::DivideByZero));
+ }
+
+ #[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)]);
+ }
+}