| 1 | # try_or [](https://travis-ci.org/Mixthos/try_or) |
| 2 | |
| 3 | Contains the macros `try_or!`, `try_or_else!`, `try_opt_or!` and |
| 4 | `try_opt_or_else!`. These are helper macros for unwrapping a `Result` or an |
| 5 | `Option` while returning early for `Err` and `None` values. The semantics |
| 6 | are similar to `unwrap_or` and `unwrap_or_else`. |
| 7 | |
| 8 | If you want a `try_opt!` macro, there's already another crate (`try_opt`) |
| 9 | for that. |
| 10 | |
| 11 | ## [Documentation](http://mixthos.github.com/try_or) |
| 12 | |
| 13 | ## Usage |
| 14 | |
| 15 | Add to your Cargo.toml: |
| 16 | |
| 17 | ```toml |
| 18 | [dependencies] |
| 19 | try_or = "0.1" |
| 20 | ``` |
| 21 | |
| 22 | Use the macros like this: |
| 23 | |
| 24 | ```rust |
| 25 | #[macro_use] |
| 26 | extern crate try_or; |
| 27 | |
| 28 | fn main() { |
| 29 | // test try_or! |
| 30 | assert_eq!({ || try_or!("5".parse::<u32>(), 1) } (), 5); |
| 31 | assert_eq!({ || try_or!("a".parse::<u32>(), 1) } (), 1); |
| 32 | |
| 33 | // test try_or_else! |
| 34 | assert_eq!({ || try_or_else!("1".parse::<u32>(), |_| 5) } (), 1); |
| 35 | assert_eq!({ || try_or_else!("a".parse::<u32>(), |_| 5) } (), 5); |
| 36 | |
| 37 | // test try_opt_or! |
| 38 | assert_eq!({ || try_opt_or!(Some(1), 2) } (), 1); |
| 39 | assert_eq!({ || try_opt_or!(None, 2) } (), 2); |
| 40 | |
| 41 | // test try_or_else! |
| 42 | assert_eq!({ || try_opt_or_else!(Some(1), || 2) } (), 1); |
| 43 | assert_eq!({ || try_opt_or_else!(None, || 2) } (), 2); |
| 44 | } |
| 45 | ``` |