-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Description
Im currently doing rustlings and struggled with iterators3 and searched a solution, which i thought i found here.
It implements one of the functions to edit like this:
fn result_with_list() -> Result<Vec<i64>, DivisionError> {
let numbers = vec![27, 297, 38502, 81];
let division_results = numbers.into_iter().map(|n| divide(n, 27));
division_results.filter(|x| x.is_ok()).collect()
}
which compiles and passes the implemented test but this didnt make any sense to me, as this can never return a correct Err variant, at "best" this would return and empty Vec, if every division returns an Err variant, which is, as far as i understand, not the intendet solution.
To remedy this, I would suggest adding test cases to cover this wrong solution, but since the functions that get tested here dont take any parameters, but instead have hardcoded values inside them, this would require changing their signature and passing in the vector of numbers to be divided.
Im new to rust, so please let me know if this isnt a real issue.
If this is indeed a real issue, im happy to suggest an updated version of this exercise.
Activity
temanmd commentedon Feb 19, 2025
As I understand, there is not an issue.
My solution was:
collect
func is smart, its result is depends on that you want to returnresult_with_list
func returnsResult<Vec<i64>, DivisionError>
andcollect()
know itlist_of_results
func returnsVec
ofResult
andcollect()
know it againcheck it https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.collect
lars-schumann commentedon Feb 19, 2025
Im not sure what your point is, my issue is that a simple "wrong" solution like the one I provided passes all tests.
lars-schumann commentedon Feb 24, 2025
To check if errors are returned properly from the 2 functions, both the numbers and the divisor have to take on several different values, this is why I moved them to be parameters of the functions and added the appropriate test cases.
Lori-Shu commentedon May 17, 2025
I may pass the test by changing the result_with_list and list_of_results to taking parameters.