Guest User

Untitled

a guest
Jul 16th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. macro_rules! chain {
  2. { $name:ident = $e:expr, $($lhs:expr => $rhs:expr),* } => {{
  3. let mut $name = $e;
  4.  
  5. loop {
  6. match true {
  7. $(_ if $lhs => { $name = $rhs })*,
  8. _ => { break },
  9. }
  10. }
  11.  
  12. $name
  13. }}
  14. }
  15.  
  16. macro_rules! branch {
  17. { $name:ident = $e:expr, $($lhs:expr => $rhs:expr),* } => {{
  18. let mut $name = $e;
  19.  
  20. match true {
  21. $(_ if $lhs => { $name = $rhs })*,
  22. _ => { },
  23. }
  24.  
  25. $name
  26. }}
  27.  
  28. }
  29.  
  30. fn main() {
  31. let x = chain! {
  32. x = 5,
  33. x == 6 => x * 2,
  34. x == 5 => x + 1,
  35. x == 7 => 0
  36. };
  37.  
  38. assert!(x == 12);
  39.  
  40. let y = branch! {
  41. y = 5,
  42. y == 6 => y * 2,
  43. y == 5 => y + 1,
  44. y == 7 => 0
  45. };
  46.  
  47. assert!(y == 6);
  48. }
Add Comment
Please, Sign In to add comment