I have an array, let's say:
let arr: Vec<f64> = vec![1.5, 1.1, -1.6, 0.9, -0.5];
I want the absolute maximum of this array:
let abs_max = arr."some functions";
println!("{}", abs_max); // Gives 1.6 (or -1.6, but not 1.5).
Is there a smart, maybe almost inline, way of doing this? Or do i have to create my own function which iterates through all the values by for loop and compares the values?
I've tried the following suggestion made in this post:
let abs_max = arr.iter().max_by(|a, b| a.abs().total_cmp(b.abs()))
And this code gives the following error:
error[E0599]: no method named `abs` found for reference `&&{float}` in the current scope
--> src\main.rs:51:44
|
51 | let abs_max = arr.iter().max_by(|a, b| a.abs().total_cmp(b.abs()));
| ^^^ method not found in `&&{float}`
error[E0599]: no method named `abs` found for reference `&&{float}` in the current scope
--> src\main.rs:51:62
|
51 | let abs_max = arr.iter().max_by(|a, b| a.abs().total_cmp(b.abs()));
| ^^^ method not found in `&&{float}`