#utility #testing #macro

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

#2659 in Rust patterns

Download history 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 959/week @ 2025-01-15 1602/week @ 2025-01-22 1578/week @ 2025-01-29 2014/week @ 2025-02-05 1665/week @ 2025-02-12 2446/week @ 2025-02-19 1688/week @ 2025-02-26 2395/week @ 2025-03-05 2112/week @ 2025-03-12 2620/week @ 2025-03-19 2237/week @ 2025-03-26

9,559 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.

No runtime deps