possibly

Crates.iopossibly
lib.rspossibly
version1.0.0
sourcesrc
created_at2024-06-17 22:21:15.005194
updated_at2024-06-17 22:21:15.005194
descriptionLike matches!(), but returning an Option type.
homepage
repositoryhttps://github.com/Hakhenaton/main.git
max_upload_size
id1274943
size2,331
RNZ (Hakhenaton)

documentation

README

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
);
Commit count: 26

cargo fmt