Crates.io | bind_match |
lib.rs | bind_match |
version | 0.1.2 |
source | src |
created_at | 2021-01-03 18:53:45.650378 |
updated_at | 2021-01-06 19:48:18.698682 |
description | Convenience macro similar to `matches!` but binds to variables in the pattern and returns an `Option` of the result. |
homepage | https://github.com/peterjoel/bind_match |
repository | https://github.com/peterjoel/bind_match |
max_upload_size | |
id | 331150 |
size | 6,389 |
Convenience macro similar to matches!
but lets you bind to and pattern and return an Option
of the result.
The input of the macro looks like this:
bind_match!(input_expr, pattern => binding_expr)
Or with pattern guards:
bind_match!(input_expr, pattern if guard => binding_expr)
The binding_expr
is returned, with variables bound in the pattern.
This can be particularly useful when matching inside iterator adapters.
use bind_match::bind_match;
enum Foo {
A(Option<i32>, bool),
B { open: bool, closed: bool },
}
struct Bar {
foo: Foo,
fun: bool,
}
fn fun_when_open(bars: impl IntoIterator<Item = Bar>) -> Option<bool> {
bars.into_iter()
.filter_map(|bar| bind_match!(bar, Bar { foo: Foo::B { open, .. }, fun } if open => fun ))
.next()
}