On this page
  1. BTreeMap
  2. Signature & Overview
  3. Notes & Pitfalls
  4. Example

BTreeMap

Prev: `HashSet` | Up: collections | Next: `VecDeque`
Rel: collections, rust/builtins
Ref: Rust std — BTreeMap
Tags: #public #prog #rust #stdlib #collections #tier/standard


Signature & Overview

BTreeMap<K, V>

Sorted B-Tree map supporting $O(\log n)$ lookups and fast .range(a..b) queries.

Notes & Pitfalls

Example

use std::collections::BTreeMap;

fn main() {
    let mut scores = BTreeMap::from([("charlie", 70), ("ada", 98), ("bob", 85)]);
    scores.entry("bob").and_modify(|v| *v += 5);
    let ordered: Vec<_> = scores.into_iter().collect();
    assert_eq!(ordered, vec![("ada", 98), ("bob", 90), ("charlie", 70)]);
}

Linked from

Related notes