#options #pattern-matching #enums #returning #match #value

possibly

Like matches!(), but returning an Option type

1 stable release

1.0.0 Jun 17, 2024

#19 in #returning

Download history 82/week @ 2024-06-25 4/week @ 2024-07-02 3/week @ 2024-08-13 7/week @ 2024-08-20 38/week @ 2024-08-27 14/week @ 2024-09-03 25/week @ 2024-09-10 19/week @ 2024-09-17 24/week @ 2024-09-24 5/week @ 2024-10-01 6/week @ 2024-10-08

61 downloads per month
Used in 7 crates (3 directly)

MIT license

3KB

possibly

A single exported macro called possibly!(), which works std::matches!() but allowing to return a value when the values matches.

It can be useful if you want to use pattern matching without dealing with extra cases.

The result is wrapped inside an Option.

Usage

use possibly::possibly;

enum MyEnum {
    Foo(u32),
    Bar
}

let value = MyEnum::Foo(1);

// basic usage with simple match arm
assert_eq!(
    possibly!(value, MyEnum::Foo(b) => b), 
    Some(1)
);

// with match arm condition
assert_eq!(
    possibly!(value, MyEnum::Foo(b) if i > 5 => b),
    None
);

No runtime deps