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 2194
盖世英雄少女心
盖世英雄少女心 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:53

    I took the answer from @Akiner Alkan and tweaked it a bit, here's a simple one-liner without any unwrap, doing the job:

    let maxi = my_vec.iter().enumerate().fold((0, 0.0), |max, (ind, &val)| if val > max.1 {(ind, val)} else {max});
    

    (PS: new to rust and first post in StackOverflow, don't judge me if I do it wrong :D)

提交回复
热议问题