O'Reilly logo

Mastering Rust by Vesa Kaihlavirta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 8 - Macros

Metaprogramming can be used to augment the features of the programming language, or just to move some computation to compile time. The most stable form of metaprogramming in Rust is in the form of macros-by-example. These are created by the macro! keyword, and they allow limited generation of Rust code from templates at compile time.

The compiler can be instructed to output generated code (the --pretty expanded parameter). On the nightly compiler, there are further debugging macros which allow finer grained debugging of the macros.

Macros are used everywhere in Rust's standard library. For instance, the function for printing text to the screen is actually a macro, because it needs compile-time information about its parameters ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required