1
1
// iterators3.rs
2
2
// This is a bigger exercise than most of the others! You can do it!
3
3
// 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 .
7
7
// Execute `rustlings hint iterators3` to get some hints!
8
- // Have fun :-)
9
8
10
9
// I AM NOT DONE
11
10
@@ -21,16 +20,28 @@ pub struct NotDivisibleError {
21
20
divisor : i32 ,
22
21
}
23
22
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.
27
25
pub fn divide ( a : i32 , b : i32 ) -> Result < i32 , DivisionError > { }
28
26
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
+
29
41
#[ cfg( test) ]
30
42
mod tests {
31
43
use super :: * ;
32
44
33
- // Tests that verify your `divide` function implementation
34
45
#[ test]
35
46
fn test_success ( ) {
36
47
assert_eq ! ( divide( 81 , 9 ) , Ok ( 9 ) ) ;
@@ -57,22 +68,16 @@ mod tests {
57
68
assert_eq ! ( divide( 0 , 81 ) , Ok ( 0 ) ) ;
58
69
}
59
70
60
- // Iterator exercises using your `divide` function
61
- /*
62
71
#[ 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])" ) ;
68
74
}
69
75
70
76
#[ 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
+ ) ;
76
82
}
77
- */
78
83
}
0 commit comments