11// iterators3.rs
22// This is a bigger exercise than most of the others! You can do it!
33// Here is your mission, should you choose to accept it:
4- // 1. Complete the divide function to get the first four tests to pass
5- // 2. Uncomment the last two tests and get them to pass by filling in
6- // values for `x` using `division_results` .
4+ // 1. Complete the divide function to get the first four tests to pass.
5+ // 2. Get the remaining tests to pass by completing the result_with_list and
6+ // list_of_results functions .
77// Execute `rustlings hint iterators3` to get some hints!
8- // Have fun :-)
98
109// I AM NOT DONE
1110
@@ -21,16 +20,28 @@ pub struct NotDivisibleError {
2120 divisor : i32 ,
2221}
2322
24- // This function should calculate `a` divided by `b` if `a` is
25- // evenly divisible by b.
26- // Otherwise, it should return a suitable error.
23+ // Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
24+ // Otherwise, return a suitable error.
2725pub fn divide ( a : i32 , b : i32 ) -> Result < i32 , DivisionError > { }
2826
27+ // Complete the function and return a value of the correct type so the test passes.
28+ // Desired output: Ok([1, 11, 1426, 3])
29+ fn result_with_list ( ) -> ( ) {
30+ let numbers = vec ! [ 27 , 297 , 38502 , 81 ] ;
31+ let division_results = numbers. into_iter ( ) . map ( |n| divide ( n, 27 ) ) ;
32+ }
33+
34+ // Complete the function and return a value of the correct type so the test passes.
35+ // Desired output: [Ok(1), Ok(11), Ok(1426), Ok(3)]
36+ fn list_of_results ( ) -> ( ) {
37+ let numbers = vec ! [ 27 , 297 , 38502 , 81 ] ;
38+ let division_results = numbers. into_iter ( ) . map ( |n| divide ( n, 27 ) ) ;
39+ }
40+
2941#[ cfg( test) ]
3042mod tests {
3143 use super :: * ;
3244
33- // Tests that verify your `divide` function implementation
3445 #[ test]
3546 fn test_success ( ) {
3647 assert_eq ! ( divide( 81 , 9 ) , Ok ( 9 ) ) ;
@@ -57,22 +68,16 @@ mod tests {
5768 assert_eq ! ( divide( 0 , 81 ) , Ok ( 0 ) ) ;
5869 }
5970
60- // Iterator exercises using your `divide` function
61- /*
6271 #[ test]
63- fn result_with_list() {
64- let numbers = vec![27, 297, 38502, 81];
65- let division_results = numbers.into_iter().map(|n| divide(n, 27));
66- let x //... Fill in here!
67- assert_eq!(format!("{:?}", x), "Ok([1, 11, 1426, 3])");
72+ fn test_result_with_list ( ) {
73+ assert_eq ! ( format!( "{:?}" , result_with_list( ) ) , "Ok([1, 11, 1426, 3])" ) ;
6874 }
6975
7076 #[ test]
71- fn list_of_results () {
72- let numbers = vec![27, 297, 38502, 81];
73- let division_results = numbers.into_iter().map(|n| divide(n, 27));
74- let x //... Fill in here!
75- assert_eq!(format!("{:?}", x), "[Ok(1), Ok(11), Ok(1426), Ok(3)]" );
77+ fn test_list_of_results ( ) {
78+ assert_eq ! (
79+ format! ( "{:?}" , list_of_results ( ) ) ,
80+ "[Ok(1), Ok(11), Ok(1426), Ok(3)]"
81+ ) ;
7682 }
77- */
7883}
0 commit comments