Macro assert2::let_assert

source ·
macro_rules! let_assert {
    ($($tokens:tt)*) => { ... };
}
Expand description

Assert that an expression matches a pattern.

This is very similar to assert!(let pattern = expression), except that this macro makes all placeholders available in the calling scope. This can be used to assert a pattern match, and then run more checks on the captured variables.

For example:

let_assert!(Ok(foo) = Foo::try_new("bar"));
check!(foo.name() == "bar");

let_assert!(Err(Error::InvalidName(e)) = Foo::try_new("bogus name"));
check!(e.name() == "bogus name");
check!(e.to_string() == "invalid name: bogus name");