get the max value of an array in rust

To get the maximum value of an array in Rust, you can use the iter() method to turn the array into an iterator, then use the max() method to get the maximum value. Here's an example:

main.rs
fn main() {
    let arr = [1, 4, 2, 3, 5];
    let max_val = arr.iter().max().unwrap();
    println!("The maximum value of the array is {}", max_val);
}
153 chars
6 lines

In this example, arr is an array of integers. We call the iter() method on the array to turn it into an iterator, and then call the max() method on the iterator to get the maximum value. Finally, we use unwrap() to get the maximum value out of the Option<i32> type that max() returns.

This code will output:

main.rs
The maximum value of the array is 5
36 chars
2 lines

gistlibby LogSnag