1 Going meta

 

This chapter covers

  • What metaprogramming is
  • Metaprogramming in Rust
  • When to use macros
  • What this book will teach you

Macros are some of the most important and powerful tools Rust has to offer. Because they have powers that Rust’s normal tooling (like functions) lacks, they can serve as “a light in dark places when all other lights go out.” That of itself is enough to make macros a core topic of this book. They have another neat quality though: they can be a pathway to other abilities. When you want to write a macro, you need knowledge of testing and debugging. You have to know how to set up a library because you cannot write a procedural macro without creating a library. Some knowledge about Rust internals, compilation, types, code organization, pattern matching, and parsing also comes in handy. Thus, teaching about macros allows me to talk about a variety of other programming topics. We will be learning about Rust macros and using them to explore other subjects.

But we are getting ahead of ourselves. Let’s take a step back and start from the beginning.

1.1 A day in the life of a Rust developer

1.2 What is metaprogramming?

1.3 Metaprogramming in Rust

1.3.1 Macro galore

1.3.2 Appropriate use cases

1.3.3 Unfit for purpose: When not to use macros

1.4 Approach of this book

Exercise

Summary