File tree Expand file tree Collapse file tree 1 file changed +19
-1
lines changed Expand file tree Collapse file tree 1 file changed +19
-1
lines changed Original file line number Diff line number Diff line change @@ -221,6 +221,10 @@ macro_rules! debug_assert_ne {
221
221
/// Like in a `match` expression, the pattern can be optionally followed by `if`
222
222
/// and a guard expression that has access to names bound by the pattern.
223
223
///
224
+ /// If the pattern is followed by a `=>` and a further mapping expression, an
225
+ /// [`Option`] will be produced, optionally containing the value produced by
226
+ /// evaluating the mapping expression in the context of the pattern.
227
+ ///
224
228
/// # Examples
225
229
///
226
230
/// ```
@@ -229,6 +233,14 @@ macro_rules! debug_assert_ne {
229
233
///
230
234
/// let bar = Some(4);
231
235
/// assert!(matches!(bar, Some(x) if x > 2));
236
+ ///
237
+ /// enum Baz {
238
+ /// A(bool),
239
+ /// B(i32),
240
+ /// }
241
+ ///
242
+ /// let baz = Baz::A(true);
243
+ /// assert_eq!(matches!(baz, Baz::A(x) => !x), Some(false));
232
244
/// ```
233
245
#[ macro_export]
234
246
#[ stable( feature = "matches_macro" , since = "1.42.0" ) ]
@@ -238,7 +250,13 @@ macro_rules! matches {
238
250
$( $pattern ) |+ $( if $guard ) ? => true ,
239
251
_ => false
240
252
}
241
- }
253
+ } ;
254
+ ( $expression: expr, $( $pattern: pat ) |+ $( if $guard: expr ) ? $( , ) ? => $mapping: expr) => {
255
+ match $expression {
256
+ $( $pattern ) |+ $( if $guard ) ? => $crate:: option:: Option :: Some ( $mapping) ,
257
+ _ => $crate:: option:: Option :: None
258
+ }
259
+ } ;
242
260
}
243
261
244
262
/// Unwraps a result or propagates its error.
You can’t perform that action at this time.
0 commit comments