Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Maximum value of a vector

  • max
  • match
  • iter
fn main() {
    let numbers = vec![10, 29, 3, 47, 8, 19];
    println!("{:?}", &numbers);
    let max = numbers.iter().max();
    println!("{:?}", max);

    match max {
        Some(&max_value) => println!("Maximum is {}", max_value),
        None => println!("The vector was empty!"),
    }

    let numbers: Vec<i32> = vec![];
    println!("{:?}", &numbers);
    let max = numbers.iter().max();
    println!("{:?}", max);

    match max {
        Some(&max_value) => println!("Maximum is {}", max_value),
        None => println!("The vector was empty!"),
    }
}
[10, 29, 3, 47, 8, 19]
Some(47)
Maximum is 47
[]
None
The vector was empty!