On this page
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
- Always sorted by key ($O(\log n)$): Supports efficient range queries (
map.range(a..=b)) and deterministic iteration order. - Requires
K: Ord(notHash): Great when you need sorted output or range scans without a separate sort step.
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
- tuts/rust/std/collections/HashSet Linked here; links to this page; shared tags: #collections, #prog, #rust, #stdlib, #tier
- tuts/rust/std/collections/VecDeque Linked here; links to this page; shared tags: #collections, #prog, #rust, #stdlib, #tier
- tuts/rust/std/collections Linked here; links to this page; shared tags: #collections, #prog, #rust, #stdlib
- tuts/rust/builtins Linked here; shared tags: #prog, #rust
- tuts/rust/std/collections/BinaryHeap shared tags: #collections, #prog, #rust, #stdlib, #tier
- tuts/rust/std/collections/HashMap shared tags: #collections, #prog, #rust, #stdlib, #tier
- tuts/rust/std/error_and_fmt/Display shared tags: #prog, #rust, #stdlib, #tier
- tuts/rust/std/error_and_fmt/Error shared tags: #prog, #rust, #stdlib, #tier
- tuts/rust/std/error_and_fmt/format shared tags: #prog, #rust, #stdlib, #tier
- tuts/rust/std/ffi/CString shared tags: #prog, #rust, #stdlib, #tier