Add and search work in a very similar manner: follow the links to the characters of the key and return the "value" in the end:
pub fn find(&mut self, path: &str) -> Option<IoTDevice> {
let mut path = path.chars();
if let Some(start) = path.next() {
self.root.get(&start).map_or(None, |mut n| {
for c in path {
match n.next.get(&c) {
Some(ref tmp) => n = tmp,
None => break,
}
}
n.value.clone()
})
} else {
None
}
}
Since the trie does not store strings in any particular order (or even consistently), getting the same data out in a predictable way is tricky! Walking it like a binary tree works well enough, but will only be deterministic with respect to the insertion order, something that should be kept in mind when testing the implementation:
pub fn walk(&self, callback: impl Fn(&IoTDevice) -> ()...