Macro variants

As you can see, it's not as complicated as you might think. As I wrote, you can have almost any pattern to the left of the macro_rules!{} side. Not only that, you can also have multiple patterns, as if it were a match statement, so that if one of them matches, it will be the one expanded. Let's see how this works by creating a macro which, depending on how we call it, will add one or two to the given integer:

macro_rules! add {    {one to $input:expr} => ($input + 1);    {two to $input:expr} => ($input + 2);}fn main() {    println!("Add one: {}", add!(one to 25/5));    println!("Add two: {}", add!(two to 25/5));}

You can see a couple of clear changes to the macro. First, we swapped braces for parentheses and parentheses for braces in the ...

Get Rust High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.