Macro static_cond::static_cond [] [src]

macro_rules! static_cond {
    (@go $lhs:tt $rhs:tt $arm1:tt $arm2:tt) => { ... };
    (if $lhs:tt == $rhs:tt $then:tt) => { ... };
    (if $lhs:tt != $rhs:tt $then:tt) => { ... };
    (if $lhs:tt == $rhs:tt $then:tt else $els:tt) => { ... };
    (if $lhs:tt != $rhs:tt $then:tt else $els:tt) => { ... };
}

Evaluates a conditional during macro expansion.

Currently limited to equality comparison. Can compare any two token trees. Can be nested.

This currently is suitable for use in expression context only (because it creates a {} scope). That could be changed, but then it wouldn't be usable in expression context.

Examples

let x = static_cond!(if (+ 1 [2 3]) == (+ 1 [2 3]) {
    static_cond!(if black != white {
        "ok"
    } else {
        the compiler will never even try to interpret this
    })
} else {
    blah blah blah blah blah unreachable
});
assert_eq!(x, "ok");

The actual conditional and the code provided for the branches not followed is eliminated after macro expansion (check rustc --pretty=expanded).