Veykril/tlborm

The Little Book of Rust Macros (updated fork)

HTML

942

158 commits

updated Jan 23, 2026

See the code

See what people are saying

SourceMessageScoreDate

It is! Although they are being used in a really weird and not normal way here. I found "The Little Book of Rust Macros"[1] to be a really good resource for getting started with Rust's declarative macros. [1]: https://veykril.github.io/tlborm/

on Solving Advent of Code at compile time with Rust macros

0

Dec 15, 2024

Incremental TT munchers: perhaps the most powerful macro parsing technique in Ru

1

Dec 9, 2024

> changing variadic C++ macros into arithmetic expressions I don't know your exact use case but it sounds a little bit hard to me. I guess you want to change things like this. ```cpp add_macro(a, b, c, d); // before a + b + c + d; // after ``` It isn't that easy to do in pattern syntax. The…

on AST-grep(sg) is a CLI tool for code structural search, lint, and rewriting

0

Dec 11, 2023

A surprising source of slow compile times can be declarative macros in Rust [0]. I believe the core of the problem is that it has to reparse the code to pattern match for the macro. One egregious patter is tt-munchers [1] where your macro is implemented recursively, requiring it to reparse the…

on A practical comparison of build and test speed between C++ and Rust

0

Jan 6, 2023

I think we've largely settled on "doing clever things is bad, actually." At least, I have. Your code should be obvious and self-documenting. Relying on language features to do a bunch of stuff behind the scenes more often leads to programs that are hard to debug and perform badly. How many times…

on Dynamic types have the potential to be more than "no static types"

0

Nov 29, 2022

There's much cooler approach: https://veykril.github.io/tlborm/decl-macros/building-blocks.... macro_rules! count { () => (0); ($odd:tt ($a:tt $b:tt)*) => ((count!($($a)*) (count!($($a)*) It uses bit twiddling to avoid generating very big AST and eventually crashing the compiler. Though I have to…

on Self Modifying Code as an alternative to macros

0

Mar 29, 2022

The Little Book of Rust Macros[0] is also worth a look imho, it helped me a lot when I started learning macros. [0] https://veykril.github.io/tlborm/introduction.html

on Rust 1.59.0 with inline assembly support etc.

0

Feb 25, 2022

The Little Book of Rust Macros - https://veykril.github.io/tlborm/introduction.html.

on A Review of the Zig Programming Language (Using Advent of Code 2021)

0

Dec 28, 2021

The little book of Rust macros (updated)

3

Nov 22, 2021

Note that this version of the book has not been updated since 2016. The updated version is here: https://veykril.github.io/tlborm/introduction.html

on The Little Book of Rust Macros

0

Nov 21, 2021

README

The Little Book of Rust Macros

Note: This is a continuation of Daniel Keep's Book which has not been updated since the early summer of 2016, adapted to make use of mdBook.

View the rendered version here and the repository here.

A chinese version of this book can be found here.

This book is an attempt to distill the Rust community's collective knowledge of Rust macros, the Macros by Example ones as well as procedural macros(WIP). As such, both additions (in the form of pull requests) and requests (in the form of issues) are very much welcome. If something's unclear, opens up questions or is not understandable as written down, fear not to make an issue asking for clarification. The goal is for this book to become the best learning resource possible.

The original Little Book of Rust Macros has helped me immensely with understanding Macros by Example style macros while I was still learning the language. Unfortunately, the original book hasn't been updated since April of 2016, while the Rust language as well as its macro-system keeps evolving. Which is why I took up the task to update the book and keep it updated as well as I can while also adding newfound things to it. In hopes that it will help out all the fresh faces coming to Rust understanding its macro systems, a part of the language a lot of people tend to have trouble with.

This book expects you to have basic knowledge of Rust, it will not explain language features or constructs that are irrelevant to macros. No prior knowledge of macros is assumed. Having read and understood the first seven chapters of the Rust Book is a must, though having read the majority of the book is recommended.

Thanks

A big thank you to Daniel Keep for the original work as well as all the contributors that added to the original which can be found here.

License

This work inherits the licenses of the original, hence it is licensed under both the Creative Commons Attribution-ShareAlike 4.0 International License and the MIT license.

macro
macros
macros-by-example
mbe
rust
rust-macros

Veykril/tlborm

The Little Book of Rust Macros (updated fork)

HTML

942

158 commits

updated Jan 23, 2026

See the code

See what people are saying

SourceMessageScoreDate

It is! Although they are being used in a really weird and not normal way here. I found "The Little Book of Rust Macros"[1] to be a really good resource for getting started with Rust's declarative macros. [1]: https://veykril.github.io/tlborm/

on Solving Advent of Code at compile time with Rust macros

0

Dec 15, 2024

Incremental TT munchers: perhaps the most powerful macro parsing technique in Ru

1

Dec 9, 2024

> changing variadic C++ macros into arithmetic expressions I don't know your exact use case but it sounds a little bit hard to me. I guess you want to change things like this. ```cpp add_macro(a, b, c, d); // before a + b + c + d; // after ``` It isn't that easy to do in pattern syntax. The…

on AST-grep(sg) is a CLI tool for code structural search, lint, and rewriting

0

Dec 11, 2023

A surprising source of slow compile times can be declarative macros in Rust [0]. I believe the core of the problem is that it has to reparse the code to pattern match for the macro. One egregious patter is tt-munchers [1] where your macro is implemented recursively, requiring it to reparse the…

on A practical comparison of build and test speed between C++ and Rust

0

Jan 6, 2023

I think we've largely settled on "doing clever things is bad, actually." At least, I have. Your code should be obvious and self-documenting. Relying on language features to do a bunch of stuff behind the scenes more often leads to programs that are hard to debug and perform badly. How many times…

on Dynamic types have the potential to be more than "no static types"

0

Nov 29, 2022

There's much cooler approach: https://veykril.github.io/tlborm/decl-macros/building-blocks.... macro_rules! count { () => (0); ($odd:tt ($a:tt $b:tt)*) => ((count!($($a)*) (count!($($a)*) It uses bit twiddling to avoid generating very big AST and eventually crashing the compiler. Though I have to…

on Self Modifying Code as an alternative to macros

0

Mar 29, 2022

The Little Book of Rust Macros[0] is also worth a look imho, it helped me a lot when I started learning macros. [0] https://veykril.github.io/tlborm/introduction.html

on Rust 1.59.0 with inline assembly support etc.

0

Feb 25, 2022

The Little Book of Rust Macros - https://veykril.github.io/tlborm/introduction.html.

on A Review of the Zig Programming Language (Using Advent of Code 2021)

0

Dec 28, 2021

The little book of Rust macros (updated)

3

Nov 22, 2021

Note that this version of the book has not been updated since 2016. The updated version is here: https://veykril.github.io/tlborm/introduction.html

on The Little Book of Rust Macros

0

Nov 21, 2021

README

The Little Book of Rust Macros

Note: This is a continuation of Daniel Keep's Book which has not been updated since the early summer of 2016, adapted to make use of mdBook.

View the rendered version here and the repository here.

A chinese version of this book can be found here.

This book is an attempt to distill the Rust community's collective knowledge of Rust macros, the Macros by Example ones as well as procedural macros(WIP). As such, both additions (in the form of pull requests) and requests (in the form of issues) are very much welcome. If something's unclear, opens up questions or is not understandable as written down, fear not to make an issue asking for clarification. The goal is for this book to become the best learning resource possible.

The original Little Book of Rust Macros has helped me immensely with understanding Macros by Example style macros while I was still learning the language. Unfortunately, the original book hasn't been updated since April of 2016, while the Rust language as well as its macro-system keeps evolving. Which is why I took up the task to update the book and keep it updated as well as I can while also adding newfound things to it. In hopes that it will help out all the fresh faces coming to Rust understanding its macro systems, a part of the language a lot of people tend to have trouble with.

This book expects you to have basic knowledge of Rust, it will not explain language features or constructs that are irrelevant to macros. No prior knowledge of macros is assumed. Having read and understood the first seven chapters of the Rust Book is a must, though having read the majority of the book is recommended.

Thanks

A big thank you to Daniel Keep for the original work as well as all the contributors that added to the original which can be found here.

License

This work inherits the licenses of the original, hence it is licensed under both the Creative Commons Attribution-ShareAlike 4.0 International License and the MIT license.

macro
macros
macros-by-example
mbe
rust
rust-macros

Significant stargazers

(top 24 of 46)

tesuji

50 followers · starred Oct 2020

Kyle Barron

1,318 followers · starred Apr 2025

Weihang Lo

636 followers · starred Nov 2024

Nadrieril

139 followers · starred Nov 2023