-
Book Overview & Buying
-
Table Of Contents
Rust for C++ Developers
By :
Rust offers two types of macros, with the first and simplest being declarative macros. Declarative macros are also sometimes called "Macros by example" in Rust documentation. While many of the capabilities of C++ macros can be achieved with Rust's declarative macros, in keeping with Rust design goals, they are safer, more hygienic, and more expressive.
In this section, we'll learn how to define and use declarative macros by working through a practical example. We'll then explore some oddities and best practices around these kinds of macros.
Rust uses the ! symbol after an identifier to denote a macro invocation. We've already encountered this syntax many times in previous chapters, and indeed it is one of the first things a Rust programmer encounters in the language due to common operations such as println! and vec! being implemented as macros by the standard library. To dive deeply into...