assert_matches2

Crates.ioassert_matches2
lib.rsassert_matches2
version0.1.2
sourcesrc
created_at2023-06-07 23:54:20.604422
updated_at2023-11-01 16:44:30.925301
descriptionA version of the assert_matches! macro that brings variables from the pattern into scope
homepage
repositoryhttps://codeberg.org/jplatte/assert_matches2
max_upload_size
id885085
size21,758
Jonas Platte (jplatte)

documentation

README

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");
Commit count: 0

cargo fmt