#pattern #error-message #macro #pattern-match

matches2

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

4 stable releases

Uses old Rust 2015

1.2.1 Nov 24, 2019
1.1.0 Jun 27, 2018
1.0.1 Jun 11, 2018
1.0.0 Jun 10, 2018

#1569 in Rust patterns

Download history 8/week @ 2024-06-15 8/week @ 2024-06-22 8/week @ 2024-06-29 4/week @ 2024-07-13 1/week @ 2024-07-20 50/week @ 2024-07-27 8/week @ 2024-08-03 1/week @ 2024-08-10 2/week @ 2024-08-24 5/week @ 2024-08-31 21/week @ 2024-09-21 48/week @ 2024-09-28

69 downloads per month
Used in 4 crates (2 directly)

MIT license

11KB
121 lines

matches2

Crates.io License

This is a fork of the matches crate with an extra unwrap_match! and option_match! macro, and also better error messages for assert_matches.

Documentation

unwrap_match! macro

The unwrap_match! macro is a general unwrap, used as such:

	let output = unwrap_match!(input, AnEnum::Variant(a) | AnEnum::OtherVariant(a) if a < 5 * 2 => a);

If it fails, it emits a descriptive error including the pattern and the input, for this reason input must implement Debug, unless you provide a custom error message as the last arguments, in the same way you'd use format!.

option_match! macro

The option_match! macro is like unwrap_match!, except instead of failing, it just returns an Option.

	let output: Option<_> = option_match!(input, AnEnum::Variant(a) | AnEnum::OtherVariant(a) if a < 5 * 2 => a);

Error message improvements

The original matches crate would emit horrible errors when assertions failed, outputting a pattern such as Some(_) as Some ( _ ). This version has properly formatted errors, so you will never experience this again.

No runtime deps