Previously, we have mostly used predefined macros—it's now time to look at creating custom macros. There are several types of macros in Rust—derive-based, function-like, and attributes, all of which have their own respective use cases. In this recipe, we'll experiment with the function-like variety to get started.
Building custom macros in Rust
How to do it...
You are only a few steps from creating macros:
- Run cargo new custom-macros in Terminal (or PowerShell on Windows) and open the directory with Visual Studio Code.
- Open src/main.rs in the editor. Let's create a new macro called one_plus_one at the top of the file:
// A simple macro without arguments
macro_rules! one_plus_one {
() =>...