I have tried using HashMap
and BTreeMap
for this but neither have worked:
use std::collections::{BTreeMap, HashMap};
fn main() {
let mut btreemap = BTreeMap::new();
println!("BTreeMap");
btreemap.insert("Z", "1");
btreemap.insert("T", "2");
btreemap.insert("R", "3");
btreemap.insert("P", "4");
btreemap.insert("K", "5");
btreemap.insert("W", "6");
btreemap.insert("G", "7");
btreemap.insert("C", "8");
btreemap.insert("A", "9");
btreemap.insert("D", "0");
for (key, value) in btreemap {
println!("{} {}", key, value);
}
println!("Hash Map");
let mut hashmap = HashMap::new();
hashmap.insert("Z", "1");
hashmap.insert("T", "2");
hashmap.insert("R", "3");
hashmap.insert("P", "4");
hashmap.insert("K", "5");
hashmap.insert("W", "6");
hashmap.insert("G", "7");
hashmap.insert("C", "8");
hashmap.insert("A", "9");
hashmap.insert("D", "0");
for (key, value) in hashmap {
println!("{} {}", key, value);
}
}
When I run this via the Rust playground, I get a result that is not sorted by order of insertion; BTreeMap
appears to be ordered alphabetically (prints A C D G K P R T W Z
, along with the numbers) and HashMap
seems to be ordered randomly (prints Z A C D R P T G WK
).
I've looked through the Rust standard library documentation and I don't see any other maps.