simple-bind: one-line non-exhaustive binds in Rust
// Here's a brief example that demonstrates how simple-bind works.
extern crate simple_bind;
use bind;
Setup
Use of this crate uses the unstable proc_macro
API, so it requires nightly and a few feature gates.
Enable nightly on your repository:
rustup override set nightly
Add this line to your cargo.toml
:
[dependencies]
simple-bind = "0.1.0"
To your main module file (lib.rs
or main.rs
), add:
#![feature(proc_macro, pattern_parentheses)]
extern crate simple_bind;
Then wherever you want to use the macro, use use
(not #[macro_use]
):
use simple_bind::bind;
Examples
fn main() {
enum B { Quux(i32) };
enum A { Foo(i32), Bar{y: i32}, Baz(B) };
// Simple binds
bind!{let A::Foo(x) = A::Foo(10);}
// Struct binds
let s = A::Bar{y: 1};
bind!{let A::Bar{y} = s;}
// Nested binds
bind!{let A::Baz(B::Quux(x)) = A::Baz(B::Quux(10));}
// Reference binds
let s = A::Foo(10);
bind!{let &mut A::Foo(ref mut y) = &mut s;}
*y = 3;
}