diff --git a/src/libstd/macros.rs b/src/libstd/macros.rs index 4db15d2cbbe0b..b9da6747e8096 100644 --- a/src/libstd/macros.rs +++ b/src/libstd/macros.rs @@ -322,6 +322,32 @@ macro_rules! vec( ($($e:expr),+,) => (vec!($($e),+)) ) +/// Return whether the given expression matches the given patterns. +/// +/// # Example +/// +/// ``` +/// let input: &str = /* ... */; +/// if !input.is_empty() && matches!(input.char_at(0), '+' | '-') +/// && matches!(input.char_at(1), '0'..'9') { +/// // Parse signed number +/// } +/// ``` +#[macro_export] +macro_rules! matches( + ($expression: expr, $($pattern:pat)|*) => ( + match $expression { + $($pattern: pat)|+ => true, + _ => false + } + ); + ($expression: expr, $($pattern:pat)|* if $guard: expr) => ( + match $expression { + $($pattern: pat)|+ if $guard => true, + _ => false + } + ); +) /// A macro to select an event from a number of receivers. ///