3 releases (breaking)

0.3.0 Feb 23, 2019
0.2.0 Feb 23, 2019
0.1.0 Feb 23, 2019

#1417 in Rust patterns

Download history 942/week @ 2023-12-14 271/week @ 2023-12-21 533/week @ 2023-12-28 1298/week @ 2024-01-04 915/week @ 2024-01-11 1143/week @ 2024-01-18 926/week @ 2024-01-25 553/week @ 2024-02-01 1278/week @ 2024-02-08 1109/week @ 2024-02-15 1456/week @ 2024-02-22 1746/week @ 2024-02-29 1498/week @ 2024-03-07 1017/week @ 2024-03-14 317/week @ 2024-03-21 276/week @ 2024-03-28

3,457 downloads per month

MIT/Apache

10KB
60 lines

multi_try

crates.io badge Docs.rs Build Status

This crate allows you to combine multiple Result types and return either a tuple containing all of their results, or a Vec of any errors which occurred. It is useful when you want to provide an error message for all errors rather than simply returning the first error.

Generics are used to support Result<T, E> for any types of T and E. The Ok types of the combined results are NOT required to be the same, but all of the Err types must be the same.

The Documentation

Example

use multi_try::MultiTry;

struct A {
    b: Result<i32, MyErr>,
    c: Result<i64, MyErr>,
    d: Result<f32, MyErr>,
}

struct ValidatedA {
    b: i32,
    c: i64,
    d: f32,
}

enum MyErr {
    FailedB,
    FailedC,
    FailedD,
}

fn validate(a: A) -> Result<ValidatedA, Vec<MyErr>> {
    let (b, c, d) = a.b.and_try(a.c).and_try(a.d)?;

    Ok(ValidatedA { b, c, d })
}

Check the tests directory for additional examples.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps