Macro expecting::expect_some

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

Expects that the given Option is Some and returns its unwrapped contents; otherwise returns early.

§Examples

fn passing_test() -> Result<()> {
    let option = Some(69);
    let contents = expect_some!(option);
    expect_eq!(contents, 69);
    Ok(())
}

fn failing_test() -> Result<()> {
    let option = None::<()>;
    let contents = expect_some!(option);  // returns early with Err
    Ok(())  // won't be reached
}

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

[my/file.rs:12] “expect_some!(option)” Expected Some, got None“
[ … wrapped Err details …]