First of all, if your a , b and c are specific values, you can simply use match :
fn main() { let x = 42; match x { 1 | 2 | 3 => println!("foo"), 42 => println!("bar"), _ => println!("nope"), } }
If you want to combine variables, you need to write match weapons as follows:
match x { x if x == a || x == b || x == c => println!("foo"), 42 => println!("bar"), _ => println!("nope"), }
... basically this is what you want to avoid.
But: direct translation of your C macro is also possible!
macro_rules! elem { ($val:expr, $($var:expr),*) => { $($val == $var)||* } } fn main() { let y = 42; let x = 42; if elem!(x, 1, 3, y) { println!("{}", x); } }
source share