Attribute Macro tryvial::try_fn

source · []
#[try_fn]
Expand description

An attribute macro that performs “Ok-wrapping” on the return value of a fn item. This is compatible with Result, Option, ControlFlow, and any type that implements the unstable [std::ops::Try] trait.

Using this macro is equivalent to wrapping the body of a fn in a try block.

Nightly:

fn fallible_fn(x: T) -> Result<U, E> {
    try {
        let a = do_one(x)?;
        let b = do_two(a)?;
        b
    }
}

With try_fn:

#[try_fn]
fn fallible_fn(x: T) -> Result<U, E> {
    let a = do_one(x)?;
    let b = do_two(a)?;
    b
}