Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code shared from the Rust Playground
macro_rules! assert_matches {
($e:expr, $($pat:pat)|+ $(if $cond:expr)?) => {
match $e {
$($pat)|+ $(if $cond)? => (),
ref e => panic!("assertion failed: `{:?}` does not match `{}`",
e, stringify!($($pat)|+ $(if $cond)?))
}
};
}
fn main() {
assert_matches!(Some(3), Some(_));
assert_matches!(Some(3), Some(3) | None | Some(2));
assert_matches!(Err(3), Ok(x) | Err(x) if x >= 3);
assert_matches!(&[3, 4][..], &[3, x] | &[x, 3] if x > 4);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.