How to write a macro in Rust to match any element in a set? -


in c, i'm used having:

if (elem(value, a, b, c)) { ... } 

which macro variable number of arguments avoid typing out

if (value == || value == b || value == c) { ... } 

a c example can seen in varargs `elem` macro use c.

is possible in rust? assume use match. if so, how variadic arguments used achieve this?

macro_rules! cmp {     // hack rust v1.11 , prior.     (@as_expr $e:expr) => { $e };      ($lhs:expr, $cmp:tt $($rhss:expr),*) => {         // bind `$lhs` name don't evaluate multiple         // times.  use leading underscore avoid unused variable warning         // in degenerate case of no `rhs`s.         match $lhs { _lhs => {             false || $(                 cmp!(@as_expr _lhs $cmp $rhss)             ) || *         //    ^- used *separator* between terms         }}     };      // same, "all".     ($lhs:expr, $cmp:tt $($rhss:expr),*) => {         match $lhs { _lhs => {             true && $( cmp!(@as_expr _lhs $cmp $rhss) ) && *         }}     }; }  fn main() {     let value = 2;     if cmp!(value, == 1, 2, 3) {         println!("true! value: {:?}", value);     }     if cmp!(value*2, != 5, 7, 1<<7 - 1) {         println!("true! value: {:?}", value);     } } 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -