Skip to content

Commit bd3d9ac

Browse files
authored
Merge pull request #649 from apogeeoak/iterator3
Enabled iterators3.rs to run without commented out tests.
2 parents aa0db83 + c6712df commit bd3d9ac

File tree

2 files changed

+34
-25
lines changed

2 files changed

+34
-25
lines changed

exercises/standard_library_types/iterators3.rs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
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.
2725
pub 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)]
3042
mod 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
}

info.toml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -725,11 +725,15 @@ name = "iterators3"
725725
path = "exercises/standard_library_types/iterators3.rs"
726726
mode = "test"
727727
hint = """
728-
Minor hint: In each of the two cases in the match in main, you can create x with either
729-
a 'turbofish' or by hinting the type of x to the compiler. You may try both.
728+
The divide function needs to return the correct error when even division is not
729+
possible.
730730
731-
Major hint: Have a look at the Iter trait and at the explanation of its collect function.
732-
Especially the part about Result is interesting."""
731+
The division_results variable needs to be collected into a collection type.
732+
733+
The result_with_list function needs to return a single Result where the success
734+
case is a vector of integers and the failure case is a DivisionError.
735+
736+
The list_of_results function needs to return a vector of results."""
733737

734738
[[exercises]]
735739
name = "iterators4"

0 commit comments

Comments
 (0)