Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created August 12, 2025 11:47
Show Gist options
  • Save rust-play/0dcd4078ef9b4c0fb7a95af7c71a58d7 to your computer and use it in GitHub Desktop.
Save rust-play/0dcd4078ef9b4c0fb7a95af7c71a58d7 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
use std::collections::BTreeMap;
fn main() {
// Let's create a nested B-tree structure:
// A BTreeMap where keys are strings (e.g., "country")
// and values are another BTreeMap (e.g., "city" -> population).
let mut world_populations: BTreeMap<String, BTreeMap<String, u64>> = BTreeMap::new();
// Add some data for "USA"
let mut usa_cities = BTreeMap::new();
usa_cities.insert("New York".to_string(), 8_468_000);
usa_cities.insert("Los Angeles".to_string(), 3_898_000);
world_populations.insert("USA".to_string(), usa_cities);
// Add some data for "India"
let mut india_cities = BTreeMap::new();
india_cities.insert("Mumbai".to_string(), 20_667_000);
india_cities.insert("Delhi".to_string(), 32_941_000);
world_populations.insert("India".to_string(), india_cities);
// Accessing the data
if let Some(cities) = world_populations.get("USA") {
if let Some(population) = cities.get("New York") {
println!("The population of New York is: {}", population);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment