How to find the longest string in an array of strings with Rust

1 Answer

0 votes
fn main() {
    let arr = ["rust", "compiler", "documentation", "packages"];

    let longest_string = arr.iter().max_by_key(|s| s.len()).copied();

    match longest_string {
        Some(s) => println!("{}", s),
        None => println!("Array is empty"), // Where the array is empty
    }
}

   
/*
run:
  
documentation
  
*/

 



answered Dec 18, 2024 by avibootz
...