feat: completed solutions
This commit is contained in:
@@ -11,21 +11,36 @@ enum DivisionError {
|
||||
// TODO: Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
|
||||
// Otherwise, return a suitable error.
|
||||
fn divide(a: i64, b: i64) -> Result<i64, DivisionError> {
|
||||
todo!();
|
||||
match (a, b) {
|
||||
(_, 0) => Err(DivisionError::DivideByZero),
|
||||
(a, b) if a == i64::MIN && b == -1 => Err(DivisionError::IntegerOverflow),
|
||||
(a, b) if a % b != 0 => Err(DivisionError::NotDivisible),
|
||||
(a, b) => Ok(a / b),
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Add the correct return type and complete the function body.
|
||||
// Desired output: `Ok([1, 11, 1426, 3])`
|
||||
fn result_with_list() {
|
||||
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));
|
||||
let result: Vec<i64> = numbers
|
||||
.into_iter()
|
||||
.map(|n| divide(n, 27))
|
||||
.collect::<Result<Vec<i64>, DivisionError>>()?;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
// 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() {
|
||||
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));
|
||||
let division_results: Vec<Result<i64, DivisionError>> = numbers
|
||||
.into_iter()
|
||||
.map(|n| divide(n, 27))
|
||||
.collect::<Vec<Result<i64, DivisionError>>>();
|
||||
|
||||
division_results
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
Reference in New Issue
Block a user