#macro #testing #utility

assert_ok

A macro that asserts a Result is Ok

3 stable releases

1.0.2 Nov 17, 2022
1.0.1 Mar 17, 2022

#2556 in Rust patterns

Download history 152/week @ 2023-08-15 309/week @ 2023-08-22 255/week @ 2023-08-29 409/week @ 2023-09-05 323/week @ 2023-09-12 503/week @ 2023-09-19 390/week @ 2023-09-26 574/week @ 2023-10-03 469/week @ 2023-10-10 655/week @ 2023-10-17 521/week @ 2023-10-24 328/week @ 2023-10-31 212/week @ 2023-11-07 197/week @ 2023-11-14 162/week @ 2023-11-21 181/week @ 2023-11-28

812 downloads per month

Apache-2.0 OR MIT

3KB

assert_ok

crates.io docs.rs

This crate contains a macro assert_ok which takes an expression and if the expression evaluates to an Err, panics with a useful message. If in contrast the expression evaluates to Ok(v) then it returns the value v.

This is commonly useful in tests. Instead of

let z = foo(arg1, arg2).unwrap();

or

let z = foo(arg1, arg2).expect("foo failed");

use

let z = assert_ok!(foo(arg1, arg2));

It's easier to understand (IMO) and more importantly provides a much more useful error message in the case that it fails.

There is a similar macro in Tokio, however for libraries or applications that don't use Tokio, pulling it in for a single macro doesn't make sense.


lib.rs:

A macro that asserts that a Result is [Ok]

No runtime deps