Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- macro_rules! chain {
- { $name:ident = $e:expr, $($lhs:expr => $rhs:expr),* } => {{
- let mut $name = $e;
- loop {
- match true {
- $(_ if $lhs => { $name = $rhs })*,
- _ => { break },
- }
- }
- $name
- }}
- }
- macro_rules! branch {
- { $name:ident = $e:expr, $($lhs:expr => $rhs:expr),* } => {{
- let mut $name = $e;
- match true {
- $(_ if $lhs => { $name = $rhs })*,
- _ => { },
- }
- $name
- }}
- }
- fn main() {
- let x = chain! {
- x = 5,
- x == 6 => x * 2,
- x == 5 => x + 1,
- x == 7 => 0
- };
- assert!(x == 12);
- let y = branch! {
- y = 5,
- y == 6 => y * 2,
- y == 5 => y + 1,
- y == 7 => 0
- };
- assert!(y == 6);
- }
Add Comment
Please, Sign In to add comment