An example-driven, step-by-step guide to success with Rust macros.
In Write Powerful Rust Macros you’ll learn how to use these amazing metaprogramming tools to push Rust to its full potential. This hands-on guide takes you from the absolute basics to advanced macro techniques, exploring Rust macros through interesting and engaging examples.
Inside Write Powerful Rust Macros you’ll
Writing declarative macros Procedural macros Reading and debugging macro code Improving the type system with newtypes and zero-sized types How common Rust libraries use macros Write Powerful Rust Macros teaches you how to write, test, debug, and publish macros for Rust. It’s perfect for Rust practitioners who want to master this powerful development technique. Build your knowledge chapter-by-chapter. You’ll start with declarative macros before diving into the real procedural macros that can generate code, augment data structures, and even create domain-specific languages.
About the technology
Macros are one of Rust’s most important and powerful tools. Although notoriously challenging, this metaprogramming technique has a big payoff. Using macros to generate new Rust code at compile-time can save you hours of tedious coding with negligible runtime performance impact. This book shows you exactly how to master this Rust superpower.
About the book
Write Powerful Rust Macros opens up the world of macros to intermediate Rust programmers. You’ll start with declarative macros to get the basics under your belt. Then, you’ll advance to procedural macros as you automatically generate a builder, learn to create your own domain-specific languages, and more. As you go, you’ll develop practical skills like testing macros, integrating macros with crates, and even sharing your macros with other developers.
What's inside
How Rust libraries use macros Reading and debugging macro code newtypes and the type state pattern About the reader
For intermediate Rust programmers.
About the author
Sam Van Overmeire is an experienced Rust developer and the author of multiple books, scientific articles and blog posts.
The technical editor on this book was Andrew Lilley Brinker.
Table of contents
1 Going meta 2 Declarative macros 3 A “Hello, World” procedural macro 4 Making fields public with attribute macros 5 Hiding information and creating mini-DSLs with function-like macros 6 Testing a builder macro 7 From panic to Error handling 8 Builder with attributes 9 Writing an infrastructure DSL 10 Macros and the outside world
It covers the matter quite thoroughly, although not all of the procedural macros methods are used (e.g. Fold, Visit, and VisitMut were left aside because they would probably require another volume). It also introduces interesting concepts like TDD, Rust's "state machine" types, Infrastructure as Code, and all sort of questions the programmer is likely to face when writing macros or even Rust code in general.
The book shows the reader how to write macros within the Rust ecosystem, by testing the code thoroughly and with the appropriate tools, giving clear error messages, documenting the crate properly, structuring the project, and so on.
Finally, it comes with a full set of exercises and a GitHub repository that contains all the code.
The title may sound niche, but Macros are hugely important part of the Rust language -- without it, you won't be able to understand many standard functions or popular libraries. And learning it will up your game significantly.
The book itself is well written -- starts easy and addresses practical examples, and accompanies each chapter with a snippet from existing open source library that demonstrates the concept. I haven't seen this level of real-worldness in other books, and I find it very good.
Rust macros are complex, when you learn the language, it looks like black magic. From code generation to DSL there are so many applications for macros in rust. The explanations are clear and the examples are interesting. While reading it, I implemented many of the techniques presented. This is THE book to read after the introduction books.