I’m trying to create a function which will return all strings matching a given prefix and their associated value, my two approaches so far are:
use std::collections::BTreeMap;
fn main() {
test_1();
test_2();
// getting the "next" character is fallible
let a = 'a' as u32;
println!("{}", a);
println!("{:?}", char::from_u32(a));
}
fn test_1() {
let mut map = BTreeMap::new();
map.insert(String::from("aa"), 1);
map.insert(String::from("aab"), 2);
map.insert(String::from("aac"), 4);
map.insert(String::from("ab"), 5);
map.insert(String::from("ba"), 6);
let n_values: Vec<(String, i32)> = map.range(String::from("aa")..String::from("ab")).map(|(s, x)| (s.clone(), *x)).collect();
println!("{:?}", n_values);
}
fn test_2() {
let mut map = BTreeMap::new();
map.insert(Vec::from(['a', 'a']), 1);
map.insert(Vec::from(['a', 'a', 'b']), 2);
map.insert(Vec::from(['a', 'a', 'c']), 4);
map.insert(Vec::from(['a', 'b']), 5);
map.insert(Vec::from(['b', 'a']), 5);
let n_values: Vec<(Vec<char>, i32)> = map.range(Vec::from(['a', 'a'])..Vec::from(['a', 'b'])).map(|(s, x)| (s.clone(), *x)).take(2).collect();
println!("{:?}", n_values)
}
However, in both cases I a range from the current characters in the string to the string with the final character replaced with the “next” character in an encoding scheme which is fallible. Is there a way to get the correct range without needing to find the “next” character or some other workaround I can find to get the desired functionality.
Thanks