[][src]Macro matches2::unwrap_match

macro_rules! unwrap_match {
    ($expression:expr, $($pattern:pat)|* $(if $ifguard:expr)? => $result:expr) => { ... };
    ($expression:expr, $($pattern:pat)|* $(if $ifguard:expr)? => $result:expr, $($msg:tt)+) => { ... };
}

A general version of Option::unwrap for all enum variants.

Syntax: unwrap_match!( expression , pattern => result [ , error message ] )

The macro evaluates to result if pattern matches, otherwise it panics with the error message or a default one that contains the pattern in it. NB: The error message is passed through to panic! verbatim, so you can do unwrap_match!(..., "{}", 2).

Examples

#[macro_use]
extern crate matches2;

#[derive(Debug)]
pub enum Foo<T> {
    A(T),
    B(T),
}

fn main() {
    let foo = Foo::B(4);
    let i = unwrap_match!(foo, Foo::B(i) | Foo::A(i) if i < 100 => i, "An error message, omittable");
    assert_eq!(i, 4);
}