Use BTreeMap for sorted key-value pairs and BTreeSet for sorted unique values, both requiring types that implement the Ord trait.
use std::collections::{BTreeMap, BTreeSet};
fn main() {
let mut map = BTreeMap::new();
map.insert("apple", 1);
map.insert("banana", 2);
let mut set = BTreeSet::new();
set.insert(10);
set.insert(5);
println!("Map: {:?}", map);
println!("Set: {:?}", set);
}
Both structures automatically keep data sorted by key or value, unlike HashMap or HashSet which do not guarantee order.