Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iterators3 tests dont check if errors are returned correctly #2207

Open
lasumn opened this issue Feb 16, 2025 · 2 comments
Open

iterators3 tests dont check if errors are returned correctly #2207

lasumn opened this issue Feb 16, 2025 · 2 comments
Labels
A-exercises Area: Exercises C-enhancement Category: Enhancement P-medium Priority: Medium

Comments

@lasumn
Copy link

lasumn commented Feb 16, 2025

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.

@temanmd
Copy link

temanmd commented Feb 19, 2025

As I understand, there is not an issue.

My solution was:

// 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> {
    match (a, b) {
        (_, 0) => Err(DivisionError::DivideByZero),
        (i64::MIN, -1) => Err(DivisionError::IntegerOverflow),
        _ if a % b != 0 => Err(DivisionError::NotDivisible),
        _ => 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() -> Result<Vec<i64>, DivisionError> {
    let numbers = [27, 297, 38502, 81];
    numbers.into_iter().map(|n| divide(n, 27)).collect()
}

// 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() -> Vec<Result<i64, DivisionError>> {
    let numbers = [27, 297, 38502, 81];
    numbers.into_iter().map(|n| divide(n, 27)).collect()
}

collect func is smart, its result is depends on that you want to return
result_with_list func returns Result<Vec<i64>, DivisionError> and collect() know it
list_of_results func returns Vec of Result and collect() know it again
check it https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.collect

@lasumn
Copy link
Author

lasumn commented 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.

@mo8it mo8it added C-enhancement Category: Enhancement A-exercises Area: Exercises P-medium Priority: Medium labels Feb 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-exercises Area: Exercises C-enhancement Category: Enhancement P-medium Priority: Medium
Projects
None yet
Development

No branches or pull requests

3 participants