I have a vector of prices (f64
). I would like to compute the highest price.
What is the current easiest and most idiomatic way to compute the max of a collection of f64
in rust ?
There has been some discussion about Ord
and f64
but I am not sure what is the most up-to-date and less hacky way to do so.
I rely on the following but I imagined there was some built in operation
let max = prices.iter().fold(None, |r, &n| match r {
Some(p) => Some(f64::max(p, n)),
None => Some(e),
});
(which is just a fold for some free monoid)
As of Rust 1.43, you can write this:
my_iterator.fold(f64::NEG_INFINITY, f64::max)
Explanation: use f64::NEG_INFINITY
as the initial value, as it is the neutral element for the f64::max
operation.