I got "reached the recursion limit ..." error for the following code. Please help.
enum Node<Item> {
Leaf(Item),
Children(Vec<Node<Item>>),
}
impl<It> Node<It> {
fn traverse(&self, f: impl Fn(&It)) {
match self {
Node::Leaf(item) => {
f(item);
}
Node::Children(children) => {
for node in children {
node.traverse(&f);
}
}
}
}
}
fn main() {
let tree = Node::Children(vec![
Node::Leaf(5),
Node::Leaf(4),
Node::Children(vec![Node::Leaf(3), Node::Leaf(2), Node::Children(vec![])]),
Node::Children(vec![Node::Children(vec![
Node::Children(vec![Node::Leaf(1)]),
Node::Leaf(0),
])]),
]);
tree.traverse(|x| println!("{x}"))
}