#testing #macro #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

#2608 in Rust patterns

Download history 224/week @ 2024-07-19 336/week @ 2024-07-26 318/week @ 2024-08-02 219/week @ 2024-08-09 277/week @ 2024-08-16 208/week @ 2024-08-23 236/week @ 2024-08-30 480/week @ 2024-09-06 921/week @ 2024-09-13 1269/week @ 2024-09-20 884/week @ 2024-09-27 1153/week @ 2024-10-04 1114/week @ 2024-10-11 1124/week @ 2024-10-18 3444/week @ 2024-10-25 1957/week @ 2024-11-01

7,955 downloads per month
Used in passarg

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