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 ) ]
1513pub 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.
2826pub 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+ }
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 ] ;
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
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 ] ;
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
4655}
4756
4857#[ cfg( test) ]
0 commit comments