The algorithm for inserting a string into a trie can be described in only a few sentences: go through each character of the word and trace it down the trie. If a node does not yet exist, create it, and add the object with the last entry.
Of course, there are special cases that need to be decided as well: what happens when a string already exists? Overwrite or ignore? In the case of this implementation, the last write will win—that is, it's overwriting whatever existed previously:
pub fn add(&mut self, device: IoTDevice) {
let p = device.path.clone();
let mut path = p.chars();
if let Some(start) = path.next() {
self.length += 1;
let mut n = self.root
.entry(start)
.or_insert(Node::new(start, None));
for c in path {
let tmp = n.next
.entry(c)
.or_insert(Node::new(c, None));
n = tmp;
}
n.value = Some(device);
}
}
Another special...