solve compile error

This commit is contained in:
Lori-Shu 2025-05-17 20:54:56 +08:00
parent 6b317822c5
commit 7b3edfe0c0

View File

@ -29,20 +29,20 @@ fn result_with_list(numbers:Vec<i64>,b:i64) -> Result<Vec<i64>, DivisionError> {
let division_results = numbers.into_iter().map(|n| divide(n, b)); let division_results = numbers.into_iter().map(|n| divide(n, b));
// Collects to the expected return type. Returns the first error in the // Collects to the expected return type. Returns the first error in the
// division results (if one exists). // division results (if one exists).
match division_results.find(|n|{ match division_results.find(|n| match n {
match n {
Ok(num) => return false, Ok(num) => return false,
Err(e)=> return true Err(e) => return true,
}
}) { }) {
Some(r) => return Err(r.unwrap_err()), Some(r) => return Err(r.unwrap_err()),
None => { None => {
let ans:Vec<i64>=division_results.map(|n|{ let ans: Vec<i64> = division_results
.map(|n| {
return match n { return match n {
Ok(num) => return num, Ok(num) => return num,
Err(e)=> return -1 Err(e) => return -1,
} };
}).collect(); })
.collect();
return Ok(ans); return Ok(ans);
} }
} }
@ -90,11 +90,17 @@ mod tests {
#[test] #[test]
fn test_result_with_list() { fn test_result_with_list() {
assert_eq!(result_with_list(vec![27, 297, 38502, 81],27), [1, 11, 1426, 3]); assert_eq!(
result_with_list(vec![27, 297, 38502, 81], 27).unwrap(),
[1, 11, 1426, 3]
);
} }
#[test] #[test]
fn test_list_of_results() { fn test_list_of_results() {
assert_eq!(list_of_results(vec![27, 297, 38502, 81],27), [Ok(1), Ok(11), Ok(1426), Ok(3)]); assert_eq!(
list_of_results(vec![27, 297, 38502, 81], 27),
[Ok(1), Ok(11), Ok(1426), Ok(3)]
);
} }
} }