std-method
slice::binary_search_by_key
Search records ordered by the same key used for lookup. The result is an index or insertion position, not the record itself. Expected stdout (not executed): book Common mistake: Sorting by name and searching by numeric ID.
Formal Syntax & Structure
rust syntaxrust
slice.binary_search_by_key(&key, |item| item_key)Working Example
Example Snippetrust
fn main() { let products = [(10, "pen"), (20, "book"), (30, "bag")]; if let Ok(i) = products.binary_search_by_key(&20, |p| p.0) { println!("{}", products[i].1); }}