#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

#2618 in Rust patterns

Download history 1100/week @ 2024-09-25 881/week @ 2024-10-02 1106/week @ 2024-10-09 1338/week @ 2024-10-16 2252/week @ 2024-10-23 2953/week @ 2024-10-30 1318/week @ 2024-11-06 1780/week @ 2024-11-13 1889/week @ 2024-11-20 1023/week @ 2024-11-27 1885/week @ 2024-12-04 1582/week @ 2024-12-11 1041/week @ 2024-12-18 222/week @ 2024-12-25 978/week @ 2025-01-01 1711/week @ 2025-01-08

4,272 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