I counted 61 characters for my closure body and 34 for the one that calls min.
I only noticed because I went looking for what the standard library already had. Here is the whole thing:
impl Solution {
pub fn find_min(nums: Vec<i32>) -> i32 {
// iterative
nums.iter().fold(
i32::MAX,
|curr_min, num| if *num < curr_min { *num } else { curr_min },
)
}
}
The seed is i32::MAX. The fold carries the smallest value seen so far, and each step compares the current element against it. That is the correct algorithm for the problem.
Then I read core/src/cmp.rs and found this:
fn min(self, other: Self) -> Self
where
Self: Sized + [const] Destruct,
{
if other < self { other } else { self }
}
That is the entire standard library implementation. curr_min.min(*num) is the same function with the arguments reversed, and the two agree on every input I tried, including i32::MIN, i32::MAX, and arrays of equal values.
So the standard library version is:
nums.iter().fold(i32::MAX, |curr_min, num| curr_min.min(*num))
The version I shipped is longer, and it is longer in the way that matters: if *num < curr_min { *num } else { curr_min } asks the reader to prove to themselves that the branches are right, while curr_min.min(*num) names the operation and lets them move on. The hand-rolled version has to be re-derived in the reader’s head. The named version is a lookup.
Turning the lint off for a whole crate
This crate is the first one in the repository with a crate-level allow at the top of the file:
#![allow(unused)]
010-find-minimum was added at 11:52 on October 7th, and every crate after it, 011 through 022, has the same line. The crates before it, 001 through 009, do not. I added it because the compiler was telling me the solution was never used:
warning: struct `Solution` is never constructed
--> src/main.rs:6:8
warning: associated function `find_min` is never used
--> src/main.rs:9:12
Both warnings are true. main() prints Hello, world! and never calls the solution, and in a binary crate a pub function that nothing calls is dead code. I could have fixed that by having main() actually call find_min. Instead I turned the lint off for the whole crate, which is a much larger hammer than the problem called for.
I kept the attribute on every crate I wrote after that one, which is the part I would change. A lint I silenced once on purpose becomes a lint nobody reads by the third time it appears.
The right fix was one line in main().
Reimplementing it on purpose
I want to be careful about what this repository is. It is a practice repository: one Cargo project per problem, kept as a record of working through Rust problems. Writing Ord::min out longhand was not a mistake. It is what the exercise asks for, and forcing the type system to check my arithmetic is the point.
The problem statement’s own hints point at iter().min(). The hints say it returns an Option, which is true, and that you should use iter().min().unwrap(). On an empty array that panics, which the constraint 1 <= nums.len() rules out.
I wrote the fold anyway. That is fine. What I got out of it was the knowledge that the body of Ord::min is a single line, and that I had just written a four-line closure that does the same thing. Reimplementing standard library functions is exactly the drill, so what I keep from this one is a second habit: read the standard library after you have done it, so you know what you are reinventing.