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

Summary

In this chapter, we gave a short introduction to metaprogramming, and took a cursory look at the many kinds of metaprogramming Rust supports and will support. The most supported form is macros-by-example, which fully works in stable Rust. They are defined by the macro_rules! macro. Macros-by-example work in the abstract syntax tree level, which means that they do not support arbitrary expansions but require that the macro expansions are well-formed in the AST.

We looked at ways to debug macros, first by asking our compiler to output the fully expanded form (--pretty expanded). The second way to debug macros, via the macros log_syntax! and trace_macros!, requires the nightly compiler but is quite a lot more convenient.

Macros are a ...

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