9
9
// Execute `rustlings hint iterators3` or use the `hint` watch subcommand for a
10
10
// hint.
11
11
12
- // I AM NOT DONE
13
-
14
12
#[ derive( Debug , PartialEq , Eq ) ]
15
13
pub enum DivisionError {
16
14
NotDivisible ( NotDivisibleError ) ,
@@ -26,23 +24,34 @@ pub struct NotDivisibleError {
26
24
// Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
27
25
// Otherwise, return a suitable error.
28
26
pub fn divide ( a : i32 , b : i32 ) -> Result < i32 , DivisionError > {
29
- todo ! ( ) ;
27
+ if b == 0 {
28
+ return Err ( DivisionError :: DivideByZero ) ;
29
+ } else if a % b != 0 {
30
+ return Err ( DivisionError :: NotDivisible ( NotDivisibleError {
31
+ dividend : a,
32
+ divisor : b,
33
+ } ) ) ;
34
+ } else {
35
+ return Ok ( a / b) ;
36
+ }
30
37
}
31
38
32
39
// Complete the function and return a value of the correct type so the test
33
40
// passes.
34
41
// Desired output: Ok([1, 11, 1426, 3])
35
- fn result_with_list ( ) -> ( ) {
42
+ fn result_with_list ( ) -> Result < Vec < i32 > , DivisionError > {
36
43
let numbers = vec ! [ 27 , 297 , 38502 , 81 ] ;
37
- let division_results = numbers. into_iter ( ) . map ( |n| divide ( n, 27 ) ) ;
44
+ let division_results = numbers. into_iter ( ) . map ( |n| divide ( n, 27 ) ) . collect ( ) ;
45
+ division_results
38
46
}
39
47
40
48
// Complete the function and return a value of the correct type so the test
41
49
// passes.
42
50
// Desired output: [Ok(1), Ok(11), Ok(1426), Ok(3)]
43
- fn list_of_results ( ) -> ( ) {
51
+ fn list_of_results ( ) -> Vec < Result < i32 , DivisionError > > {
44
52
let numbers = vec ! [ 27 , 297 , 38502 , 81 ] ;
45
- let division_results = numbers. into_iter ( ) . map ( |n| divide ( n, 27 ) ) ;
53
+ let division_results = numbers. into_iter ( ) . map ( |n| divide ( n, 27 ) ) . collect ( ) ;
54
+ division_results
46
55
}
47
56
48
57
#[ cfg( test) ]
0 commit comments