2 stable releases
1.0.1 | Oct 9, 2019 |
---|
#2980 in Rust patterns
4KB
About
Simple crate that provides a macro to retrieve the first option that returns a value, given a list of options
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
at your option.
lib.rs
:
This crate exports a macro to chain multiple options together to retrieve the first item that is Some
, from left to right
Every item is lazily evaluated, but to make this possible there's a caveat when trying to pass closures: you have to call them in place when passing the item
Example
fn my_fn<T>() -> Option<T> { None }
fn main() {
let opt = Some(3);
let v = any_opt!((|| None)(), my_fn(), Some(13), Some(55), None, Some(42), opt).unwrap_or_default();
assert_eq!(v, 13);
}