Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. /// Given an expression `e`, add 88. If identifier `i` and statement block `blk` are specified,
  2. /// add `i` to the result and execute `blk`.
  3. macro_rules! add_88 {
  4. // If `e` is an expression like `123`, `i * 2`, `func(i)`...
  5. ($e:expr) => {
  6. {
  7. // Add 88 to the expression and return it.
  8. $e + 88
  9. }
  10. };
  11. // If `e` is an expression, `i` is an identifier like `myvar`, `blk` is a block of statements...
  12. ($e:expr, $i:ident, $blk:block) => {
  13. {
  14. // Add 88 and the value of `i` to the expression.
  15. let result = $e + $i + 88;
  16. // Execute the code block.
  17. $blk;
  18. // Return the result.
  19. result
  20. }
  21. }
  22. }
  23. ...
  24. let x = 2;
  25. let y = 3;
  26. let result = add_88!(
  27. x * 4, // Expression
  28. y, // Identifier
  29. { println!("The values of x and y are {} and {}", x, y); } // Code Block
  30. )
  31. // Shows `The value of x and y are 2 and 3`. Result is 99.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement