21.13 Matching Boxed Types

Patterns also work with pointers and smart pointers like Box<T>:

enum IntWrapper {
    Boxed(Box<i32>),
    Inline(i32),
}

fn describe_int_wrapper(wrapper: IntWrapper) {
    match wrapper {
        IntWrapper::Boxed(boxed_val) => {
            println!("Got a boxed integer: {}", boxed_val);
        }
        IntWrapper::Inline(val) => {
            println!("Got an inline integer: {}", val);
        }
    }
}

fn main() {
    let x = IntWrapper::Boxed(Box::new(10));
    let y = IntWrapper::Inline(20);
    describe_int_wrapper(x);
    describe_int_wrapper(y);
}

You can also use destructuring like Box(ref mut val) if you need a mutable reference to the contents.