Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// Given an expression `e`, add 88. If identifier `i` and statement block `blk` are specified,
- /// add `i` to the result and execute `blk`.
- macro_rules! add_88 {
- // If `e` is an expression like `123`, `i * 2`, `func(i)`...
- ($e:expr) => {
- {
- // Add 88 to the expression and return it.
- $e + 88
- }
- };
- // If `e` is an expression, `i` is an identifier like `myvar`, `blk` is a block of statements...
- ($e:expr, $i:ident, $blk:block) => {
- {
- // Add 88 and the value of `i` to the expression.
- let result = $e + $i + 88;
- // Execute the code block.
- $blk;
- // Return the result.
- result
- }
- }
- }
- ...
- let x = 2;
- let y = 3;
- let result = add_88!(
- x * 4, // Expression
- y, // Identifier
- { println!("The values of x and y are {} and {}", x, y); } // Code Block
- )
- // Shows `The value of x and y are 2 and 3`. Result is 99.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement