From 67fb6c0eb507965836201551f90c7d637bf6cee0 Mon Sep 17 00:00:00 2001 From: Hunter Kvalevog Date: Sun, 27 Sep 2026 17:28:34 -0500 Subject: rustlings: complete 18 --- rustlings/exercises/18_iterators/iterators3.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'rustlings/exercises/18_iterators/iterators3.rs') 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 { - 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, 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> { 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() { -- cgit v1.2.3