-
-
Save RandyMcMillan/0f661101bb80cc64f2fb220585bbe974 to your computer and use it in GitHub Desktop.
nested_b_tree.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=0f661101bb80cc64f2fb220585bbe974