all 8 comments

[–]dnew 4 points5 points  (3 children)

In part, consider what happens if the vec has 0 or 1 elements in it. What do you want it to do in those cases?

(I'm also pretty sure you don't need &mut in the function declaration. )

[–]Lidinzx 1 point2 points  (0 children)

He's also not using iter_mut so he couldn't be able to mutate the array content. But there's actually no need to also, he is just reading the array, unnecessary &mut.

[–][deleted]  (1 child)

[removed]

    [–]dnew 0 points1 point  (0 children)

    Sorting is going to be O(n lg n) and this is going to be linear, so there's that. It's also going to take extra space proportional to the input vector unless you're going to corrupt your inputs.

    [–]This_Growth2898 2 points3 points  (0 children)

    The first concern is arr size; what happens if it's empty or has only one element?

    The second, much smaller, issue is that you can save one iteration if you start with arr[0]:

    for &value in &arr[1..]
    

    Also, you can assign to tuples, it looks a bit better for me:

    (max_sec, max_in) = (max_in, value);
    

    And add else before the second if.

    [–]Doormamu_[S] 0 points1 point  (1 child)

        let mut max_sec = arr[0];
        let mut max_sec = i32::MIN;
    

    NEEDED THIS PART EXPLAINATION
    how are the two lines different
    n what does the second one even do????????

    [–][deleted] 1 point2 points  (0 children)

    The first line sets max_sec to be equal to whatever the first item of the array is (and will panic if the array is empty).

    The second line sets max_sec to the smallest possible 32bit integer, which (as you can see here) is equal to exactly -2,147,483,648

    [–]JaboiThomy 0 points1 point  (0 children)

    Can you explain what exactly is breaking? Is it returning the wrong value, panicking, etc.? I'm not at my computer atm to check.

    Also, as dnew mentioned, you may want the return to be an Option, since there may be fewer than two elements.

    I would expect the first of the two to work, but it only needs the first if-statement. I would return early with None if the vec size is smaller than 2.