I tried to use Rust collections such as BTreeMap to store key-value pairs for use as a sorted list, but I found that it matched the exact key only. For example, in a phone book case, I can find the item with exact key "David", but not item start with "Dav":
use std::collections::BTreeMap;
fn main() {
let mut map = BTreeMap::new();
map.insert("Daniel", "798-1364");
// FOUND WITH EXACT MATCH ONLY
// like map.get(&"Daniel"), Not Found Here
match map.get(&"Dan") {
Some(&number) => println!("Found: {}", number),
_ => println!("Not Found."),
}
}
Can I do partial matching of the string prefix using collections such as BTreeMap
?
Furthermore, if my keys are i64
s, can I find a range of items such as when the key is greater than 1000? I know how to iterate through all the items, but I want to iterate just the range of items found.
Can I access the items by index, to do binary search manually?