How to find the longest word in a string with Rust

1 Answer

0 votes
fn longest_word(s: &str) -> &str {
    s.split_whitespace()
        .max_by_key(|w| w.len())
        .unwrap_or("")
}

fn main() {
    let s = "Could you recommend a good restaurant nearby?";
    
    println!("{}", longest_word(s));
}



/*
run:

restaurant

*/

 



answered Mar 3 by avibootz
...