- max
- min
- max_by
- min_by
- cmp
Longest or shortest string in a vector
- max and min abc order
- max and min by length
examples/vectors/get-longest-string/src/main.rs
fn main() { let animals = ["snake", "mouse", "cat", "elephant"]; println!("{:?}", animals); let max = animals.iter().max().unwrap(); println!("{}", max); let min = animals.iter().min().unwrap(); println!("{}", min); let longest = animals.iter().max_by(|x, y| x.len().cmp(&y.len())).unwrap(); println!("{}", longest); let shortest = animals.iter().min_by(|x, y| x.len().cmp(&y.len())).unwrap(); println!("{}", shortest); }
["snake", "mouse", "cat", "elephant"] snake cat elephant cat