Macro expecting::expect_ok

source ·
macro_rules! expect_ok {
    ( $result:expr ) => { ... };
}
Expand description

Expects that the given Result is Ok and returns its unwrapped contents; otherwise returns early.

§Examples

fn passing_test() -> Result<()> {
    let result: Result<i32> = Ok(69);
    let contents = expect_ok!(result);
    expect_eq!(contents, 69);
    Ok(())
}

fn failing_test() -> Result<()> {
    let result: Result<i32> = Err(anyhow!("ruh roh!"));
    let contents = expect_ok!(result);  // returns early
    Ok(())  // won't be reached
}

failing_test() will return early after calling expect_ok!. The Err will be wrapped in a descriptive error message such as:

[my/file.rs:12] “expect_ok!(result)” Expected Ok, got Err: “ruh roh!”
[ … wrapped Err details …]