Okay, the question is a little more complicated because it has some constraints. So here they are:
- The vector is immutable
- The elements don't implement copy (so you need to take ownership of them to return the minimum)
- The elements are comparable with a f32
Here's what I've tried:
- std::vec::Vec implements sort_by which is good enough because my vecs are usually small. I can just use partial_cmp to compare f32s. But unfortunately due to (1), the vector is immutable and sort_by is in-place. So this won't work
- There's vec.iter().min_by_key(|x| x.my_f32) but f32 doesn't implement Ord (it implements PartialOrd) so Rust complains about a type mismatch.
So I've come up with this monstrosity, which works but seems like a huge amount of code to do something simple:
struct MyStruct {my_f32: f32}
// Takes ownership of my_vec and returns its smallest element (or None if the list is empty).
fn find_min(my_vec: Vec<MyStruct>) -> Option<MyStruct> {
let mut min: Option<MyStruct> = None;
for element in my_vec {
min = match min {
None => {
Some(element)
},
Some(min) => {
if element.my_f32 < min.my_f32 {
Some(element)
} else {
Some(min)
}
}
}
}
min
}
There has to be something I'm stupidly overlooking. Does anyone have an idea about how this can be simplified?