Skip to content

Instantly share code, notes, and snippets.

@songzhi
Last active October 9, 2021 03:13
Show Gist options
  • Save songzhi/8e9550ea23927482a837e35f40ebc13d to your computer and use it in GitHub Desktop.
Save songzhi/8e9550ea23927482a837e35f40ebc13d to your computer and use it in GitHub Desktop.
///
/// ## Examples
/// ```
/// enum Foo {
/// Path(String),
/// Count(usize),
/// }
/// let foo = Foo::Path(1);
/// let f: Option<String> = try_match!(foo, Foo::Path(x)=> x);
/// ```
macro_rules! try_match {
($expression:expr, $( $pattern:pat_param )|+ $( if $guard: expr )? => $out: expr) => {
match $expression {
$( $pattern )|+ $( if $guard )? => ::core::option::Option::Some($out),
_ => ::core::option::Option::None,
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment