Skip to content

Commit b21f9b6

Browse files
committed
Iner3 F
1 parent 2c06789 commit b21f9b6

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

exercises/iterators/iterators3.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
// Execute `rustlings hint iterators3` or use the `hint` watch subcommand for a
1010
// hint.
1111

12-
// I AM NOT DONE
13-
1412
#[derive(Debug, PartialEq, Eq)]
1513
pub enum DivisionError {
1614
NotDivisible(NotDivisibleError),
@@ -26,23 +24,34 @@ pub struct NotDivisibleError {
2624
// Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
2725
// Otherwise, return a suitable error.
2826
pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {
29-
todo!();
27+
if b == 0 {
28+
Err(DivisionError::DivideByZero)
29+
} else if a % b == 0 {
30+
Ok(a / b)
31+
} else {
32+
Err(DivisionError::NotDivisible(NotDivisibleError {
33+
dividend: a,
34+
divisor: b,
35+
}))
36+
}
3037
}
3138

3239
// Complete the function and return a value of the correct type so the test
3340
// passes.
3441
// Desired output: Ok([1, 11, 1426, 3])
35-
fn result_with_list() -> () {
42+
fn result_with_list() -> Result<Vec<i32>, DivisionError> {
3643
let numbers = vec![27, 297, 38502, 81];
3744
let division_results = numbers.into_iter().map(|n| divide(n, 27));
45+
division_results.collect()
3846
}
3947

4048
// Complete the function and return a value of the correct type so the test
4149
// passes.
4250
// Desired output: [Ok(1), Ok(11), Ok(1426), Ok(3)]
43-
fn list_of_results() -> () {
51+
fn list_of_results() -> Vec<Result<i32, DivisionError>> {
4452
let numbers = vec![27, 297, 38502, 81];
4553
let division_results = numbers.into_iter().map(|n| divide(n, 27));
54+
division_results.collect()
4655
}
4756

4857
#[cfg(test)]

0 commit comments

Comments
 (0)