|
|
@ -1,11 +1,10 @@
|
|
|
|
// iterators3.rs
|
|
|
|
// iterators3.rs
|
|
|
|
// This is a bigger exercise than most of the others! You can do it!
|
|
|
|
// This is a bigger exercise than most of the others! You can do it!
|
|
|
|
// Here is your mission, should you choose to accept it:
|
|
|
|
// Here is your mission, should you choose to accept it:
|
|
|
|
// 1. Complete the divide function to get the first four tests to pass
|
|
|
|
// 1. Complete the divide function to get the first four tests to pass.
|
|
|
|
// 2. Uncomment the last two tests and get them to pass by filling in
|
|
|
|
// 2. Get the remaining tests to pass by completing the result_with_list and
|
|
|
|
// values for `x` using `division_results`.
|
|
|
|
// list_of_results functions.
|
|
|
|
// Execute `rustlings hint iterators3` to get some hints!
|
|
|
|
// Execute `rustlings hint iterators3` to get some hints!
|
|
|
|
// Have fun :-)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// I AM NOT DONE
|
|
|
|
// I AM NOT DONE
|
|
|
|
|
|
|
|
|
|
|
@ -21,16 +20,28 @@ pub struct NotDivisibleError {
|
|
|
|
divisor: i32,
|
|
|
|
divisor: i32,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// This function should calculate `a` divided by `b` if `a` is
|
|
|
|
// Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
|
|
|
|
// evenly divisible by b.
|
|
|
|
// Otherwise, return a suitable error.
|
|
|
|
// Otherwise, it should return a suitable error.
|
|
|
|
|
|
|
|
pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {}
|
|
|
|
pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Complete the function and return a value of the correct type so the test passes.
|
|
|
|
|
|
|
|
// Desired output: Ok([1, 11, 1426, 3])
|
|
|
|
|
|
|
|
fn result_with_list() -> () {
|
|
|
|
|
|
|
|
let numbers = vec![27, 297, 38502, 81];
|
|
|
|
|
|
|
|
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Complete the function and return a value of the correct type so the test passes.
|
|
|
|
|
|
|
|
// Desired output: [Ok(1), Ok(11), Ok(1426), Ok(3)]
|
|
|
|
|
|
|
|
fn list_of_results() -> () {
|
|
|
|
|
|
|
|
let numbers = vec![27, 297, 38502, 81];
|
|
|
|
|
|
|
|
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
|
|
// Tests that verify your `divide` function implementation
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn test_success() {
|
|
|
|
fn test_success() {
|
|
|
|
assert_eq!(divide(81, 9), Ok(9));
|
|
|
|
assert_eq!(divide(81, 9), Ok(9));
|
|
|
@ -57,22 +68,16 @@ mod tests {
|
|
|
|
assert_eq!(divide(0, 81), Ok(0));
|
|
|
|
assert_eq!(divide(0, 81), Ok(0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Iterator exercises using your `divide` function
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn result_with_list() {
|
|
|
|
fn test_result_with_list() {
|
|
|
|
let numbers = vec![27, 297, 38502, 81];
|
|
|
|
assert_eq!(format!("{:?}", result_with_list()), "Ok([1, 11, 1426, 3])");
|
|
|
|
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
|
|
|
|
|
|
|
let x //... Fill in here!
|
|
|
|
|
|
|
|
assert_eq!(format!("{:?}", x), "Ok([1, 11, 1426, 3])");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn list_of_results() {
|
|
|
|
fn test_list_of_results() {
|
|
|
|
let numbers = vec![27, 297, 38502, 81];
|
|
|
|
assert_eq!(
|
|
|
|
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
|
|
|
format!("{:?}", list_of_results()),
|
|
|
|
let x //... Fill in here!
|
|
|
|
"[Ok(1), Ok(11), Ok(1426), Ok(3)]"
|
|
|
|
assert_eq!(format!("{:?}", x), "[Ok(1), Ok(11), Ok(1426), Ok(3)]");
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|