macro_rules! assert_ok { ($val:expr, $($msg:tt)+) => { ... }; ($val:expr) => { ... }; }
Available on crate feature
assert
only.Expand description
Asserts that the provided expression, that returns a Result
, is Ok
.
§Motivation
The standard pattern for asserting a value is Ok
using the assert!
macro is:
assert!(x.is_ok());
The issue with this pattern is when the assertion fails it only prints false
and not the value contained in the Err(_)
variant which makes debugging difficult.
§Examples
§Basic Use
ⓘ
use mz_ore::assert_ok;
let error: Result<usize, usize> = Err(42);
assert_ok!(error);
§With extra message
ⓘ
use mz_ore::assert_ok;
let other_val = 100;
let error: Result<usize, usize> = Err(42);
assert_ok!(error, "ohh noo! x {other_val}");