How it works...
Thanks to the compiler executing macros before the actual compilation, we can generate code that will show up in the final program but that was actually created via a macro invocation. This lets us reduce boilerplate code, enforce defaults (such as implementing certain traits, adding metadata, and many others), or simply provide a nicer interface for users of our crate.
In step 2, we are creating a simple macro to repeat a block (these curly braces—{ }—and their content are called a block) several times—using a for loop. The tests created in step 4 show how this operates and what it can do—it executes as if we were to write a for loop right in the test.
Step 3 creates a more interesting thing: a function. Together with ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access