match_t!() { /* proc-macro */ }Expand description
Pattern match on trait objects based on their concrete types.
It supports both reference (&dyn Trait) and boxed (Box<dyn Trait>)
trait objects.
Use move keyword to indicate ownership transfer when matching on Box<dyn Trait>.
§Example
ⓘ
type_enum! {
enum Tree<T: Display> {
Leaf(T),
Node(Box<Tree<T>>, Box<Tree<T>>),
}
}
let tree: Box<dyn Tree<i32>> = Box::new(...);
let tree_ref: &dyn Tree<i32> = &...;
let describe = match_t! {
move tree {
Leaf(value) => format!("Leaf: {}", value),
Node(left, right) => format!("Node with left and right"),
}
}
let describe_ref = match_t! {
tree_ref {
Leaf(value) => format!("Leaf: {}", value),
Node(left, right) => format!("Node with left and right"),
}
}