Macro assert_matches::assert_matches [−][src]
Asserts that an expression matches a given pattern.
A guard expression may be supplied to add further restrictions to the expected value of the expression.
A match
arm may be supplied to perform additional assertions or to yield
a value from the macro invocation.
Examples
#[macro_use] extern crate assert_matches; #[derive(Debug)] enum Foo { A(i32), B(&'static str), } let a = Foo::A(1); // Assert that `a` matches the pattern `Foo::A(_)`. assert_matches!(a, Foo::A(_)); // Assert that `a` matches the pattern and // that the contained value meets the condition `i > 0`. assert_matches!(a, Foo::A(i) if i > 0); let b = Foo::B("foobar"); // Assert that `b` matches the pattern `Foo::B(_)`. assert_matches!(b, Foo::B(s) => { // Perform additional assertions on the variable binding `s`. assert!(s.starts_with("foo")); assert!(s.ends_with("bar")); }); // Assert that `b` matches the pattern and yield the string `s`. let s = assert_matches!(b, Foo::B(s) => s); // Perform an assertion on the value `s`. assert_eq!(s, "foobar");