@@ -110,7 +110,10 @@ macro_rules! assert_ne {
110110 } ) ;
111111}
112112
113- /// Asserts that an expression matches a pattern.
113+ /// Asserts that an expression matches any of the given patterns.
114+ ///
115+ /// Like in a `match` expression, the pattern can be optionally followed by `if`
116+ /// and a guard expression that has access to names bound by the pattern.
114117///
115118/// On panic, this macro will print the value of the expression with its
116119/// debug representation.
@@ -125,38 +128,35 @@ macro_rules! assert_ne {
125128/// let b = 1u32.checked_sub(2);
126129/// assert_matches!(a, Some(_));
127130/// assert_matches!(b, None);
131+ ///
132+ /// let c = Ok("abc".to_string());
133+ /// assert_matches!(a, Ok(x) | Err(x) if x.len() < 100);
128134/// ```
129135#[ macro_export]
130136#[ unstable( feature = "assert_matches" , issue = "none" ) ]
131137#[ allow_internal_unstable( core_panic) ]
132138macro_rules! assert_matches {
133- ( $left: expr, $right: pat $( , ) ?) => ( {
134- match & $left {
139+ ( $left: expr, $( $pattern: pat ) |+ $( if $guard: expr ) ? $( , ) ?) => ( {
140+ match $left {
141+ $( $pattern ) |+ $( if $guard ) ? => { }
135142 left_val => {
136- if let $right = left_val {
137- // OK
138- } else {
139- $crate:: panicking:: assert_matches_failed(
140- & * left_val,
141- $crate:: stringify!( $right) ,
142- $crate:: option:: Option :: None
143- ) ;
144- }
143+ $crate:: panicking:: assert_matches_failed(
144+ & left_val,
145+ $crate:: stringify!( $( $pattern) |+ $( if $guard) ?) ,
146+ $crate:: option:: Option :: None
147+ ) ;
145148 }
146149 }
147150 } ) ;
148- ( $left: expr, $right: expr, $( $arg: tt) +) => ( {
149- match & $left {
151+ ( $left: expr, $( $pattern: pat ) |+ $( if $guard: expr ) ?, $( $arg: tt) +) => ( {
152+ match $left {
153+ $( $pattern ) |+ $( if $guard ) ? => { }
150154 left_val => {
151- if let $right = left_val {
152- // OK
153- } else {
154- $crate:: panicking:: assert_matches_failed(
155- & * left_val,
156- $crate:: stringify!( $right) ,
157- $crate:: option:: Option :: Some ( $crate:: format_args!( $( $arg) +) )
158- ) ;
159- }
155+ $crate:: panicking:: assert_matches_failed(
156+ & left_val,
157+ $crate:: stringify!( $( $pattern) |+ $( if $guard) ?) ,
158+ $crate:: option:: Option :: Some ( $crate:: format_args!( $( $arg) +) )
159+ ) ;
160160 }
161161 }
162162 } ) ;
0 commit comments