typestar

BTreeMap in Rust

An ordered map: iteration follows the keys, and you can ask for a range.

use std::collections::BTreeMap;

fn main() {
    let mut runs = BTreeMap::new();
    runs.insert("2026-07-02", 91);
    runs.insert("2026-07-29", 104);
    runs.insert("2026-07-14", 98);

    for (day, tpm) in &runs {
        println!("{day} {tpm}");
    }

    for (day, tpm) in runs.range("2026-07-10".."2026-07-20") {
        println!("mid month: {day} {tpm}");
    }

    println!("{:?}", runs.first_key_value());
}

How it works

  1. Keys stay sorted, so iteration is deterministic.
  2. range selects a slice of the key space.
  3. first_key_value reads the smallest entry.

Keywords and builtins used here

The run, in numbers

Lines
18
Characters to type
367
Tokens
115
Three-star pace
90 tpm

At the three-star pace of 90 tokens a minute, this run takes about 77 seconds.

Type this snippet

Step 7 of 8 in Collections, step 36 of 39 in Language basics.

← Previous Next →