Find the longest string
Rust
#![allow(unused)]
fn main() {
let words = vec!["cat", "elephant", "dog"];
let longest = words.iter().max_by_key(|w| w.len());
}
Python
words = ["cat", "elephant", "dog"]
longest = max(words, key=lambda w: len(w))
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
#![allow(unused)]
fn main() {
let words = vec!["cat", "elephant", "dog"];
let longest = words.iter().max_by_key(|w| w.len());
}
words = ["cat", "elephant", "dog"]
longest = max(words, key=lambda w: len(w))