std-method

BTreeMap::get_key_value

Look up a borrowed key and retrieve references to the actual stored key and value. This is useful when callers need the owned key representation without cloning it. Expected stdout (not executed): Some(("A2", 4)) Common mistake: Expecting the returned key to be the query object itself.

Formal Syntax & Structure

rust syntaxrust
fn get_key_value<Q>(&self, k: &Q) -> Option<(&K, &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("A2"), 4)]);    println!("{:?}", rooms.get_key_value("A2"));}
Rust Reference