Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created March 14, 2019 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/b8f94f0dfd15a37800e35d230892c864 to your computer and use it in GitHub Desktop.
Save rust-play/b8f94f0dfd15a37800e35d230892c864 to your computer and use it in GitHub Desktop.
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