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

前端 未结 5 2051
盖世英雄少女心
盖世英雄少女心 2021-01-18 03:09

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

T

5条回答
  •  后悔当初
    2021-01-18 03:47

    Is there a reason why this wouldn't work?

    use std::cmp::Ordering;
    
    fn example(nets: &Vec) {
        let index_of_max: Option = nets
            .iter()
            .enumerate()
            .max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(Ordering::Equal))
            .map(|(index, _)| index);
    }
    

提交回复
热议问题