Created
March 14, 2019 22:03
-
-
Save rust-play/b8f94f0dfd15a37800e35d230892c864 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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