assert_ok

Crates.ioassert_ok
lib.rsassert_ok
version1.0.2
sourcesrc
created_at2022-03-17 13:35:01.960606
updated_at2022-11-17 19:24:22.186622
descriptionA macro that asserts a Result is Ok
homepage
repositoryhttps://github.com/randombit/assert_ok
max_upload_size
id551777
size4,489
Jack Lloyd (randombit)

documentation

README

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.

Commit count: 14

cargo fmt