Skip to content

Instantly share code, notes, and snippets.

@sug0
Created April 26, 2020 19:32
Show Gist options
  • Select an option

  • Save sug0/a3e11c2f28289bd78bab26e696d1983d to your computer and use it in GitHub Desktop.

Select an option

Save sug0/a3e11c2f28289bd78bab26e696d1983d to your computer and use it in GitHub Desktop.
C style switch in Rust
macro_rules! switch {
($( $cond:expr => $result:expr ),+ $(,)?) => {
match true {
$( _ if $cond => $result ),+,
_ => unreachable!(),
}
}
}
fn main() {
let x = 32;
switch! {
x > 55 => println!("yeeeeeepppp"),
true => println!("fug"),
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment