diff options
| author | Hunter Kvalevog <hunter@kvog.sh> | 2026-09-27 17:28:34 -0500 |
|---|---|---|
| committer | Hunter Kvalevog <hunter@kvog.sh> | 2026-09-27 17:28:34 -0500 |
| commit | 67fb6c0eb507965836201551f90c7d637bf6cee0 (patch) | |
| tree | 59e405f7f98a0451d3a9051c2e2ed03dc3585dfa /rustlings/exercises/18_iterators/iterators3.rs | |
| parent | 54731f0fc0b0fde18438853c0a8abaa968edb57a (diff) | |
rustlings: complete 18
Diffstat (limited to 'rustlings/exercises/18_iterators/iterators3.rs')
| -rw-r--r-- | rustlings/exercises/18_iterators/iterators3.rs | 19 |
1 files changed, 14 insertions, 5 deletions
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() { |