Hi, all:
I want to build a tree below, but I'm stuck with the E0499.
Any hints?
#[cfg(test)]
impl Node {
fn new() -> Self {
Self { text: "".to_string(), children: vec![] }
}
fn add_child(&mut self, node: Node) {
self.children.push(node);
}
}
#[test]
fn test_node() {
// root
// |
// a ------- b
// |----| |
// a b a
let mut root = Node::new();
let vec_s = vec!["a", "b", "aa", "ab", "ba"];
for s in vec_s {
let mut p_node = &mut root;
for c in s.as_bytes() {
let idx:usize = (c - 96) as usize;
let node = p_node.children.get_mut(idx);
if node.is_some() {
p_node = node.unwrap();
} else {
p_node.add_child(Node::new());
}
}
}
}
}