Skip to content

Commit

Permalink
Added mapping support to matches!
Browse files Browse the repository at this point in the history
  • Loading branch information
zesterer committed Dec 7, 2020
1 parent 8d2d001 commit 5d7909a
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion library/core/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ macro_rules! debug_assert_ne {
/// Like in a `match` expression, the pattern can be optionally followed by `if`
/// and a guard expression that has access to names bound by the pattern.
///
/// If the pattern is followed by a `=>` and a further mapping expression, an
/// [`Option`] will be produced, optionally containing the value produced by
/// evaluating the mapping expression in the context of the pattern.
///
/// # Examples
///
/// ```
Expand All @@ -229,6 +233,14 @@ macro_rules! debug_assert_ne {
///
/// let bar = Some(4);
/// assert!(matches!(bar, Some(x) if x > 2));
///
/// enum Baz {
/// A(bool),
/// B(i32),
/// }
///
/// let baz = Baz::A(true);
/// assert_eq!(matches!(baz, Baz::A(x) => !x), Some(false));
/// ```
#[macro_export]
#[stable(feature = "matches_macro", since = "1.42.0")]
Expand All @@ -238,7 +250,13 @@ macro_rules! matches {
$( $pattern )|+ $( if $guard )? => true,
_ => false
}
}
};
($expression:expr, $( $pattern:pat )|+ $( if $guard: expr )? $(,)? => $mapping:expr) => {
match $expression {
$( $pattern )|+ $( if $guard )? => $crate::option::Option::Some($mapping),
_ => $crate::option::Option::None
}
};
}

/// Unwraps a result or propagates its error.
Expand Down

0 comments on commit 5d7909a

Please sign in to comment.