Implement the Visitor Pattern in Rust by defining a Visitor trait with methods for each node type and a visit method on your data structure that dispatches to the trait.
trait Visitor {
fn visit_node(&self, node: &Node) {}
}
enum Node {
Leaf(i32),
Branch(Vec<Node>),
}
impl Node {
fn visit<V: Visitor>(&self, v: &V) {
match self {
Node::Leaf(_) => v.visit_node(self),
Node::Branch(children) => {
for child in children {
child.visit(v);
}
}
}
}
}