3 releases

0.1.2 Dec 4, 2019
0.1.1 Dec 4, 2019
0.1.0 Dec 4, 2019

#2102 in Procedural macros

MIT license

7KB
99 lines

guard_let

doc.rs crates.io

Guard let for rust

Usage


use guard_let::guard_let;

enum Enum {
    A(String),
    B(usize),
    C(Struct),
}

struct Struct {
    foo: String,
}

fn eat_string(_: String) {}

#[guard_let]
fn simple_ident() {
    let v = Enum::A(String::from(""));

    guard_let!(v as Enum::A(s), {
        // Type of v is Enum at here.
        println!("v is not A: {:?}", v);
        return;
    });

    // Type of s is String
    eat_string(s)
}

#[guard_let]
fn pattern() {
    let v = Enum::A(String::from(""));

    guard_let!(v as Enum::C(Struct { foo }), {
        // Type of v is Enum at here.
        println!("v is not C: {:?}", v);
        return;
    });

    // Type of s is String
    eat_string(foo)
}


License

MIT

Dependencies

~1.5MB
~36K SLoC