#macro #pattern #expression #boolean

archived 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

#2590 in Rust patterns

Download history 426775/week @ 2023-08-07 414634/week @ 2023-08-14 446377/week @ 2023-08-21 468412/week @ 2023-08-28 404995/week @ 2023-09-04 425215/week @ 2023-09-11 437734/week @ 2023-09-18 423709/week @ 2023-09-25 413214/week @ 2023-10-02 428851/week @ 2023-10-09 435865/week @ 2023-10-16 429605/week @ 2023-10-23 458056/week @ 2023-10-30 456637/week @ 2023-11-06 503781/week @ 2023-11-13 377392/week @ 2023-11-20

1,817,488 downloads per month
Used in 6,237 crates (224 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