#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

#2716 in Rust patterns

Download history 1923/week @ 2024-11-16 1046/week @ 2024-11-23 1614/week @ 2024-11-30 1704/week @ 2024-12-07 1731/week @ 2024-12-14 267/week @ 2024-12-21 499/week @ 2024-12-28 1653/week @ 2025-01-04 1244/week @ 2025-01-11 1272/week @ 2025-01-18 1570/week @ 2025-01-25 1850/week @ 2025-02-01 2057/week @ 2025-02-08 2039/week @ 2025-02-15 1851/week @ 2025-02-22 1958/week @ 2025-03-01

8,190 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