#variables #pattern #macro #scope #assert #debugging #assert-matches

dev assert_matches2

A version of the assert_matches! macro that brings variables from the pattern into scope

3 releases

0.1.2 Nov 1, 2023
0.1.1 Oct 23, 2023
0.1.0 Jun 7, 2023

#576 in Rust patterns

Download history 1032/week @ 2024-07-22 558/week @ 2024-07-29 907/week @ 2024-08-05 657/week @ 2024-08-12 1019/week @ 2024-08-19 791/week @ 2024-08-26 1134/week @ 2024-09-02 818/week @ 2024-09-09 820/week @ 2024-09-16 808/week @ 2024-09-23 898/week @ 2024-09-30 1087/week @ 2024-10-07 970/week @ 2024-10-14 621/week @ 2024-10-21 655/week @ 2024-10-28 1140/week @ 2024-11-04

3,454 downloads per month
Used in 17 crates (16 directly)

MPL-2.0 license

8KB

assert_matches2

A replacement for the assert_matches crate, providing an assert_matches! macro without support for if guards and the extra arm, but bringing any names introduced in the pattern into scope for after the macro invocation, by expanding to let else.

Example

use assert_matches2::assert_matches;

/// Basic usage
let var = serde_json::Value::from("a string");
assert_matches!(var, serde_json::Value::String(s));
assert_eq!(s, "a string");

// More complex usages
#[derive(Debug)]
struct Foo {
    bar: Bar,
}

#[derive(Debug)]
enum Bar {
    V1(u8),
    V2 { field: String },
}

let var = Foo { bar: Bar::V1(10) };
assert_matches!(var, Foo { bar: ref r @ Bar::V1(int) });
assert_matches!(r, Bar::V1(_));
assert_eq!(int, 10);

let var = Foo { bar: Bar::V2 { field: "test".to_owned() } };
assert_matches!(var, Foo { bar: Bar::V2 { field: rename } });
assert_eq!(rename, "test");

No runtime deps