Pattern match a reference to a boxed enum

How do you pattern match a reference to a boxed enum?

Say I have:

#[derive(Debug)]
enum Btree {
  Empty,
  Node(i32, Box<Btree>, Box<Btree>),
}

use Btree::Empty;
use Btree::Node;

And I want to display the contents of the Btree with:

fn display(bt: &Box<Btree>) {
  match /*not sure how to patten match bt to reach Btree*/ {
    /*Btree branches here*/
  }
}

Your function should take &Btree instead of &Box<Btree>. A reference to boxed value is generally unnecessary indirection, since Box dereferences to its inner value and deref coerces automatically when passed as a function argument. You can then pattern match over the reference.

2 Likes

I was thinking that very thing, a reference to a boxed entity is unnecessary indirection... I'll have to rethink my Btree functions. Thanks for halting my faulty logic.

And now my code works as intended..

#[derive(Debug)]
enum Btree {
  Empty,
  Node(i32, Box<Btree>, Box<Btree>),
}

use Btree::Empty;
use Btree::Node;

#[allow(dead_code)]
fn empty() -> Btree {
  Empty
}

#[allow(dead_code)]
fn add(data: i32, bt: Btree) -> Btree {
  match bt {
    Empty => Node(data, Box::new(empty()), Box::new(empty())),
    Node(d, left, right) => {
      if d < data {
        Node(d, Box::new(add(data, *left)), right)
      } else if d > data {
        Node(d, left, Box::new(add(data, *right)))
      } else {
        Node(data, left, right)
      }
    }
  }
}

#[allow(dead_code)]
fn display(bt: &Btree) {
  match bt {
    Empty => println!("Empty"),
    Node(d, left, right) => {
      display(&*left);
      println!("{}", d);
      display(&*right)
    }
  }
}

fn main() {
  let bt = vec![10, 1, 9, 2, 8, 3, 7, 4, 6, 5]
    .iter()
    .fold(empty(), |acc, e| add(*e, acc));

  display(&bt);

  println!("{:#?}", bt);
}

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.