std-method

BTreeMap::get

Look up a value without consuming the key. An owned String key can be queried with a str slice; both views must agree about ordering. Expected stdout (not executed): Some(4) None Common mistake: Allocating a new String for every lookup.

Formal Syntax & Structure

rust syntaxrust
fn get<Q>(&self, key: &Q) -> Option<&V> where K: Borrow<Q> + Ord, Q: Ord + ?Sized

Working Example

Example Snippetrust
fn main() {    use std::collections::BTreeMap;    let rooms = BTreeMap::from([(String::from("A12"), 4)]);    println!("{:?}", rooms.get("A12"));    println!("{:?}", rooms.get("B01"));}
Rust Reference