std-method
Iterator::min_by_key — choose smallest key
Return the item with the least computed key; ties choose the first such item. Empty input yields None. Expected stdout (not executed): Some(("lint", 3)) Common mistake: Expecting the key itself rather than the original item.
Formal Syntax & Structure
rust syntaxrust
fn min_by_key<B, F>(self, f: F) -> Option<Self::Item> where Self: Sized, B: Ord, F: FnMut(&Self::Item) -> BWorking Example
Example Snippetrust
fn main() { let fastest = [("compile", 8), ("lint", 3), ("test", 6)].into_iter().min_by_key(|job| job.1); println!("{fastest:?}");}