#options #any #first #value #list #macro #closures

any-opt

Simple macro to get the first value from a list of options

2 stable releases

1.0.1 Oct 9, 2019

#2901 in Rust patterns

Apache-2.0/MIT

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

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);
}

No runtime deps