2016-04-19 15:56:46 -04:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2024-05-22 09:04:12 -04:00
|
|
|
enum DivisionError {
|
2016-04-19 15:56:46 -04:00
|
|
|
DivideByZero,
|
2024-06-28 09:00:13 -04:00
|
|
|
NotDivisible,
|
2016-04-19 15:56:46 -04:00
|
|
|
}
|
|
|
|
|
2024-06-28 09:00:13 -04:00
|
|
|
// TODO: Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
|
2021-02-12 15:36:53 -05:00
|
|
|
// Otherwise, return a suitable error.
|
2024-05-22 09:04:12 -04:00
|
|
|
fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {
|
2022-01-13 10:26:46 -05:00
|
|
|
todo!();
|
|
|
|
}
|
2016-04-19 15:56:46 -04:00
|
|
|
|
2024-06-28 09:00:13 -04:00
|
|
|
// TODO: Add the correct return type and complete the function body.
|
|
|
|
// Desired output: `Ok([1, 11, 1426, 3])`
|
|
|
|
fn result_with_list() {
|
|
|
|
let numbers = [27, 297, 38502, 81];
|
2021-02-12 15:36:53 -05:00
|
|
|
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
|
|
|
}
|
|
|
|
|
2024-06-28 09:00:13 -04:00
|
|
|
// TODO: Add the correct return type and complete the function body.
|
|
|
|
// Desired output: `[Ok(1), Ok(11), Ok(1426), Ok(3)]`
|
|
|
|
fn list_of_results() {
|
|
|
|
let numbers = [27, 297, 38502, 81];
|
2021-02-12 15:36:53 -05:00
|
|
|
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
|
|
|
}
|
|
|
|
|
2024-04-17 16:46:21 -04:00
|
|
|
fn main() {
|
|
|
|
// You can optionally experiment here.
|
|
|
|
}
|
|
|
|
|
2016-04-19 15:56:46 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_success() {
|
|
|
|
assert_eq!(divide(81, 9), Ok(9));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-06-28 09:00:13 -04:00
|
|
|
fn test_divide_by_0() {
|
|
|
|
assert_eq!(divide(81, 0), Err(DivisionError::DivideByZero));
|
2016-04-19 15:56:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-06-28 09:00:13 -04:00
|
|
|
fn test_not_divisible() {
|
|
|
|
assert_eq!(divide(81, 6), Err(DivisionError::NotDivisible));
|
2016-04-19 15:56:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_divide_0_by_something() {
|
|
|
|
assert_eq!(divide(0, 81), Ok(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-12 15:36:53 -05:00
|
|
|
fn test_result_with_list() {
|
2024-06-28 09:00:13 -04:00
|
|
|
assert_eq!(result_with_list().unwarp(), [1, 11, 1426, 3]);
|
2016-04-19 15:56:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-12 15:36:53 -05:00
|
|
|
fn test_list_of_results() {
|
2024-06-28 09:00:13 -04:00
|
|
|
assert_eq!(list_of_results(), [Ok(1), Ok(11), Ok(1426), Ok(3)]);
|
2016-04-19 15:56:46 -04:00
|
|
|
}
|
|
|
|
}
|