rustlings/solutions/18_iterators/iterators3.rs
2024-06-28 15:00:13 +02:00

73 lines
1.7 KiB
Rust

#[derive(Debug, PartialEq, Eq)]
enum DivisionError {
DivideByZero,
NotDivisible,
}
fn divide(a: i64, b: i64) -> Result<i64, DivisionError> {
if b == 0 {
return Err(DivisionError::DivideByZero);
}
if a % b != 0 {
return Err(DivisionError::NotDivisible);
}
Ok(a / b)
}
fn result_with_list() -> Result<Vec<i64>, DivisionError> {
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
let numbers = [27, 297, 38502, 81];
let division_results = numbers.into_iter().map(|n| divide(n, 27));
// Collects to the expected return type. Returns the first error in the
// division results (if one exists).
division_results.collect()
}
fn list_of_results() -> Vec<Result<i64, DivisionError>> {
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
let numbers = [27, 297, 38502, 81];
let division_results = numbers.into_iter().map(|n| divide(n, 27));
// Collects to the expected return type.
division_results.collect()
}
fn main() {
// You can optionally experiment here.
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_success() {
assert_eq!(divide(81, 9), Ok(9));
}
#[test]
fn test_divide_by_0() {
assert_eq!(divide(81, 0), Err(DivisionError::DivideByZero));
}
#[test]
fn test_not_divisible() {
assert_eq!(divide(81, 6), Err(DivisionError::NotDivisible));
}
#[test]
fn test_divide_0_by_something() {
assert_eq!(divide(0, 81), Ok(0));
}
#[test]
fn test_result_with_list() {
assert_eq!(result_with_list().unwrap(), [1, 11, 1426, 3]);
}
#[test]
fn test_list_of_results() {
assert_eq!(list_of_results(), [Ok(1), Ok(11), Ok(1426), Ok(3)]);
}
}