What is the idiomatic way to get the index of a maximum or minimum floating point value in a slice or Vec in Rust?

  • A+
Category:Languages

Assumption -- The Vec<f32> does not have any NaN values or exhibit any NaN behavior.

Take the following sample set:

0.28   0.3102 0.9856 0.3679 0.3697 0.46   0.4311 0.9781 0.9891 0.5052 0.9173 0.932  0.8365 0.5822 0.9981 0.9977 

What is the neatest and most stable way to get the index of the highest value in the above list (values can be negative)?

My initial attempts were along the following lines:

let _tmp = *nets.iter().max_by(|i, j| i.partial_cmp(j).unwrap()).unwrap();     let _i = nets.iter().position(|&element| element == _tmp).unwrap(); 

Where nets is a &Vec<f32>. Which to me seems blatantly incorrect.

The Python equivalent of this that works (taking into consideration the above assumption):

_i = nets.index(max(nets)) 

 


I will probably do something like this:

fn main() -> Result<(), Box<std::error::Error>> {     let samples = vec![         0.28, 0.3102, 0.9856, 0.3679, 0.3697, 0.46, 0.4311, 0.9781, 0.9891, 0.5052, 0.9173, 0.932,         0.8365, 0.5822, 0.9981, 0.9977,     ];      // Use enumerate to get the index     let mut iter = samples.iter().enumerate();     // we get the first entry     let init = iter.next().ok_or("Need at least one input")?;     // we process the rest     let result = iter.try_fold(init, |acc, x| {         // return None if x is NaN         let cmp = x.1.partial_cmp(acc.1)?;         // if x is greater the acc         let max = if let std::cmp::Ordering::Greater = cmp {             x         } else {             acc         };         Some(max)     });     println!("{:?}", result);      Ok(()) } 

This could be implemented by adding a trait on Iterator with for example the function try_max_by.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: