finish iterators3

This commit is contained in:
lukaszKielar 2020-06-30 19:56:31 +02:00
parent 4582eda5cd
commit e63c871147

View file

@ -7,8 +7,6 @@
// Execute `rustlings hint iterators3` to get some hints!
// Have fun :-)
// I AM NOT DONE
#[derive(Debug, PartialEq, Eq)]
pub enum DivisionError {
NotDivisible(NotDivisibleError),
@ -24,7 +22,18 @@ pub struct NotDivisibleError {
// This function should calculate `a` divided by `b` if `a` is
// evenly divisible by b.
// 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> {
if b == 0 {
Err(DivisionError::DivideByZero)
} else if (a % b) != 0 {
Err(DivisionError::NotDivisible(NotDivisibleError {
dividend: a,
divisor: b,
}))
} else {
Ok(a / b)
}
}
#[cfg(test)]
mod tests {
@ -58,12 +67,14 @@ mod tests {
}
// Iterator exercises using your `divide` function
/*
#[test]
fn result_with_list() {
let numbers = vec![27, 297, 38502, 81];
let division_results = numbers.into_iter().map(|n| divide(n, 27));
let x //... Fill in here!
// we need to collect Result object, where:
// - Ok -> Vec<i32>
// - Err -> DivisionError
let x = division_results.collect::<Result<Vec<_>, DivisionError>>();
assert_eq!(format!("{:?}", x), "Ok([1, 11, 1426, 3])");
}
@ -71,8 +82,10 @@ mod tests {
fn list_of_results() {
let numbers = vec![27, 297, 38502, 81];
let division_results = numbers.into_iter().map(|n| divide(n, 27));
let x //... Fill in here!
// we need to collect vector of results, where:
// - Ok -> i32
// - Err -> DivisionError
let x = division_results.collect::<Vec<Result<i32, DivisionError>>>();
assert_eq!(format!("{:?}", x), "[Ok(1), Ok(11), Ok(1426), Ok(3)]");
}
*/
}