Macro assert_matches2::assert_let
source · macro_rules! assert_let { ($pat:pat = $e:expr) => { ... }; ($pat:pat = $e:expr, $($arg:tt)*) => { ... }; }
Expand description
Alternative form of assert_matches!
where the pattern comes first.
If the assertion fails, the expression after the =
is debug-printed as
part of the panic message.
Example
use assert_matches2::assert_let;
use serde_json::{json, Value as JsonValue};
let val = json!({ "field": [1, 2, 3] });
assert_let!(JsonValue::Object(obj) = val);
assert_eq!(obj.len(), 1);
assert_let!(Some((field_name, JsonValue::Array(arr))) = obj.into_iter().next());
assert_eq!(field_name, "field");
assert_eq!(arr, [1, 2, 3]);