99// Execute `rustlings hint iterators3` or use the `hint` watch subcommand for a
1010// hint.
1111
12- // I AM NOT DONE
1312
1413#[ derive( Debug , PartialEq , Eq ) ]
1514pub enum DivisionError {
@@ -26,23 +25,33 @@ pub struct NotDivisibleError {
2625// Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
2726// Otherwise, return a suitable error.
2827pub fn divide ( a : i32 , b : i32 ) -> Result < i32 , DivisionError > {
29- todo ! ( ) ;
28+ if b == 0 {
29+ Err ( DivisionError :: DivideByZero )
30+ }
31+ else if a%b != 0 {
32+ Err ( DivisionError :: NotDivisible ( NotDivisibleError { dividend : a, divisor : b} ) )
33+ }
34+ else {
35+ 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 ] ;
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