#expression #pattern #boolean #evaluate #whether #macro #match

matches

A macro to evaluate, as a boolean, whether an expression matches a pattern

10 releases

Uses old Rust 2015

0.1.10 Jan 21, 2023
0.1.9 Aug 12, 2021
0.1.8 Aug 22, 2018
0.1.7 Jul 19, 2018
0.1.0 Dec 5, 2014

#2313 in Rust patterns

Download history 413586/week @ 2023-11-18 451119/week @ 2023-11-25 453809/week @ 2023-12-02 430085/week @ 2023-12-09 385591/week @ 2023-12-16 201284/week @ 2023-12-23 321699/week @ 2023-12-30 409569/week @ 2024-01-06 411528/week @ 2024-01-13 420579/week @ 2024-01-20 423284/week @ 2024-01-27 413782/week @ 2024-02-03 412137/week @ 2024-02-10 422180/week @ 2024-02-17 461430/week @ 2024-02-24 380581/week @ 2024-03-02

1,751,077 downloads per month
Used in 5,978 crates (222 directly)

MIT license

6KB
55 lines

A macro to evaluate, as a boolean, whether an expression matches a pattern.

For users who build using only Rust 1.42 and newer, consider using std::matches, which is included in the standard library prelude and thus is automatically in scope.


lib.rs:

A macro to evaluate, as a boolean, whether an expression matches a pattern.

For users who build using only Rust 1.42 and newer, consider using std::matches, which is included in the standard library prelude and thus is automatically in scope.

Examples

#[macro_use]
extern crate matches;

#[derive(Debug)]
pub enum Foo<T> {
    A,
    B(T),
}

impl<T> Foo<T> {
    pub fn is_b(&self) -> bool {
        matches!(*self, Foo::B(_))
    }
}

impl<T: core::fmt::Debug> Foo<T> {
    pub fn assert_is_b(&self) {
        assert_matches!(&self, Foo::B(_));
    }
}

No runtime deps