Visitor Design Pattern in Rust

1. Definition

The Visitor Design Pattern represents an operation to be performed on elements of an object structure without changing the classes on which it operates. It allows adding new operations to existing object structures without modifying them.

2. Problem Statement

You have a stable data structure but need to add new operations frequently. Directly adding these operations to the classes would lead to frequent modifications, violating the Open/Closed principle.

3. Solution

Use the Visitor pattern to separate the operations from the objects. The objects only need to provide an 'accept' method that takes a visitor as an argument. The visitor then performs the operation on the object.

4. Real-World Use Cases

1. Document rendering: Different visitors can produce HTML, PDF, or other formats.

2. Syntax tree interpretation: Different visitors can evaluate or pretty-print an abstract syntax tree.

5. Implementation Steps

1. Create an abstract visitor with a visit method for each concrete element in the object structure.

2. Each element in the object structure must have an 'accept' method that takes a visitor as a parameter.

3. Create concrete visitors implementing the abstract visitor, defining the operations for each element.

6. Implementation in Rust Programming

// Element trait
trait Element {
    fn accept(&self, visitor: &dyn Visitor);
// Concrete elements
struct ElementA;
impl Element for ElementA {
    fn accept(&self, visitor: &dyn Visitor) {
struct ElementB;
impl Element for ElementB {
    fn accept(&self, visitor: &dyn Visitor) {
// Visitor trait
trait Visitor {
    fn visit_element_a(&self, element: &ElementA);
    fn visit_element_b(&self, element: &ElementB);
// Concrete visitor
struct ConcreteVisitor;
impl Visitor for ConcreteVisitor {
    fn visit_element_a(&self, _: &ElementA) {
        println!("Visited ElementA with ConcreteVisitor");
    fn visit_element_b(&self, _: &ElementB) {
        println!("Visited ElementB with ConcreteVisitor");
// Client code
fn main() {
    let elements: Vec<Box<dyn Element>> = vec![Box::new(ElementA), Box::new(ElementB)];
    let visitor = ConcreteVisitor;
    for element in &elements {


Visited ElementA with ConcreteVisitor
Visited ElementB with ConcreteVisitor


1. The Element trait represents elements that can be visited. Each concrete element (e.g., ElementA, ElementB) implements the accept method to allow a visitor to visit it.

2. The Visitor trait provides a visiting method for each concrete element.

3. The ConcreteVisitor implements specific operations for visiting each element.

4. The client code demonstrates visiting a list of elements with a concrete visitor.

7. When to use?

Use the Visitor pattern when:

1. An object structure contains many classes and you want to perform operations on these objects without changing their classes.

2. New operations need to be added frequently, and it's preferable to keep the object structure unchanged.

3. Operations need to access the internal state of objects in the structure, but you don't want to expose this state.