2019-06-06 21:10:08 -05:00
|
|
|
// iterators3.rs
|
2016-04-19 14:56:46 -05:00
|
|
|
// This is a bigger exercise than most of the others! You can do it!
|
|
|
|
// Here is your mission, should you choose to accept it:
|
2021-02-12 14:36:53 -06:00
|
|
|
// 1. Complete the divide function to get the first four tests to pass.
|
|
|
|
// 2. Get the remaining tests to pass by completing the result_with_list and
|
|
|
|
// list_of_results functions.
|
2022-07-14 11:29:09 -05:00
|
|
|
// Execute `rustlings hint iterators3` or use the `hint` watch subcommand for a hint.
|
2016-04-19 14:56:46 -05:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
pub enum DivisionError {
|
|
|
|
NotDivisible(NotDivisibleError),
|
|
|
|
DivideByZero,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
pub struct NotDivisibleError {
|
|
|
|
dividend: i32,
|
|
|
|
divisor: i32,
|
|
|
|
}
|
|
|
|
|
2021-02-12 14:36:53 -06:00
|
|
|
// Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
|
|
|
|
// Otherwise, return a suitable error.
|
2022-01-13 09:26:46 -06:00
|
|
|
pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {
|
2023-03-24 16:18:51 -05:00
|
|
|
match b {
|
|
|
|
0 => Err(DivisionError::DivideByZero),
|
|
|
|
_ => match a % b {
|
|
|
|
0 => Ok(a / b),
|
|
|
|
_ => Err(DivisionError::NotDivisible(NotDivisibleError {
|
|
|
|
dividend: a,
|
|
|
|
divisor: b,
|
|
|
|
})),
|
|
|
|
},
|
|
|
|
}
|
2022-01-13 09:26:46 -06:00
|
|
|
}
|
2016-04-19 14:56:46 -05:00
|
|
|
|
2021-02-12 14:36:53 -06:00
|
|
|
// Complete the function and return a value of the correct type so the test passes.
|
|
|
|
// Desired output: Ok([1, 11, 1426, 3])
|
2023-03-24 16:18:51 -05:00
|
|
|
fn result_with_list() -> Result<Vec<i32>, DivisionError> {
|
2021-02-12 14:36:53 -06:00
|
|
|
let numbers = vec![27, 297, 38502, 81];
|
2023-03-24 16:18:51 -05:00
|
|
|
match numbers
|
|
|
|
.into_iter()
|
|
|
|
.map(|n| match divide(n, 27) {
|
|
|
|
Ok(r) => Ok(r),
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
{
|
|
|
|
Ok(v) => Ok(v),
|
|
|
|
Err(e) => Err(e),
|
|
|
|
}
|
2021-02-12 14:36:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)]
|
2023-03-24 16:18:51 -05:00
|
|
|
fn list_of_results() -> Vec<Result<i32, DivisionError>> {
|
2021-02-12 14:36:53 -06:00
|
|
|
let numbers = vec![27, 297, 38502, 81];
|
2023-03-24 16:18:51 -05:00
|
|
|
numbers
|
|
|
|
.into_iter()
|
|
|
|
.map(|n| match divide(n, 27) {
|
|
|
|
Ok(r) => Ok(r),
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
})
|
|
|
|
.collect()
|
2021-02-12 14:36:53 -06:00
|
|
|
}
|
|
|
|
|
2016-04-19 14:56:46 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_success() {
|
|
|
|
assert_eq!(divide(81, 9), Ok(9));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_not_divisible() {
|
|
|
|
assert_eq!(
|
|
|
|
divide(81, 6),
|
2019-06-06 21:52:42 -05:00
|
|
|
Err(DivisionError::NotDivisible(NotDivisibleError {
|
2016-04-19 14:56:46 -05:00
|
|
|
dividend: 81,
|
|
|
|
divisor: 6
|
|
|
|
}))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_divide_by_0() {
|
|
|
|
assert_eq!(divide(81, 0), Err(DivisionError::DivideByZero));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_divide_0_by_something() {
|
|
|
|
assert_eq!(divide(0, 81), Ok(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-12 14:36:53 -06:00
|
|
|
fn test_result_with_list() {
|
|
|
|
assert_eq!(format!("{:?}", result_with_list()), "Ok([1, 11, 1426, 3])");
|
2016-04-19 14:56:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-12 14:36:53 -06:00
|
|
|
fn test_list_of_results() {
|
|
|
|
assert_eq!(
|
|
|
|
format!("{:?}", list_of_results()),
|
|
|
|
"[Ok(1), Ok(11), Ok(1426), Ok(3)]"
|
|
|
|
);
|
2016-04-19 14:56:46 -05:00
|
|
|
}
|
|
|
|
}
|