A comprehensive Rust metaprogramming and macro engineering repository focused on mastering Rust compile-time systems through practical ALX-style projects.
This repository explores Rust macros from beginner fundamentals to advanced framework-level metaprogramming concepts used in real-world systems engineering, backend infrastructure, and compiler tooling.
Rust macros are one of the most powerful features in the language. They allow developers to generate code at compile time, reduce boilerplate, enforce correctness, build custom DSLs (Domain Specific Languages), and create framework-level abstractions.
This repository is designed as a structured learning and engineering lab for:
- mastering
macro_rules! - understanding token tree parsing
- building recursive macros
- learning macro hygiene
- designing compile-time DSLs
- writing procedural macros
- generating APIs and framework systems
The projects follow an ALX-inspired progression model, starting with macro basics and gradually advancing into compiler-style engineering.
By completing the projects in this repository, you will learn how to:
- Define and invoke Rust macros
- Understand macro expansion
- Work with fragment specifiers
- Parse token trees (
tt) - Use repetition patterns effectively
- Build recursive macros
- Design custom DSLs
- Debug macro expansion
- Understand macro hygiene and scoping
- Build procedural macros
- Parse Rust syntax trees using
syn - Generate code using
quote - Design framework-level compile-time systems
alx-rust-macros/
│
├── README.md
├── LICENSE
├── .gitignore
├── Cargo.toml
│
├── 0x00-macro-basics/
├── 0x01-repetition-patterns/
├── 0x02-token-tree-parsing/
├── 0x03-recursive-macros/
├── 0x04-macro-hygiene/
├── 0x05-procedural-macros/
├── 0x06-framework-projects/
│
└── docs/
Introduction to Rust macros and compile-time expansion.
macro_rules!- pattern matching
- macro templates
- fragment specifiers
- simple expansion
- hello macro
- calculator macro
- sum macro
- debug macro
- logging macro
Understanding repetition and variadic macro systems.
$(...)*$(...)+- optional trailing commas
- repeated expansion
- list parsing
- custom
vec! - variadic print macro
- array builder
- comma parser
Advanced parsing using token trees and DSL design.
ttfragments- nested parsing
- syntax design
- compile-time builders
- DSL engineering
- JSON macro
- HTML DSL
- CSS DSL
- query builder macro
Recursive compile-time systems and token processing.
- recursive expansion
- recursive parsing
- token counting
- nested evaluation
- recursive sum macro
- recursive parser
- token counter
- nested expression evaluator
Understanding variable scoping and hygienic expansion.
- hygienic macros
- scope isolation
- identifier collisions
- safe variable generation
- expansion safety
- hygiene lab
- scoped bindings
- safe temporary variables
- collision prevention systems
Advanced AST-level metaprogramming.
proc_macro- derive macros
- attribute macros
- syntax parsing
- token stream manipulation
- AST transformations
synquoteproc_macro2
derive(Display)derive(Builder)- custom attribute macros
- compile-time validators
Framework-inspired compile-time systems.
- API generation
- routing systems
- compile-time validation
- framework abstractions
- backend DSLs
- mini router
- mini serde
- mini actix
- compile-time API generator
- Rust
- Cargo
- Cargo Workspaces
macro_rules!- procedural macros
synquoteproc_macro2
Before starting this repository, you should understand:
- Rust ownership and borrowing
- functions and modules
- enums and structs
- traits and generics
- pattern matching
- iterators and collections
git clone <repository-url>
cd alx-rust-macroscargo buildExample:
cargo run -p hello_macrocargo testThis repository heavily encourages learning macro debugging techniques.
Install:
cargo install cargo-expandUse:
cargo expandtrace_macros!(true);log_syntax!();This repository is designed around the idea that:
Macro engineering is compiler engineering.
The goal is not only to write macros, but to:
- understand how Rust parses syntax
- think in tokens and expansions
- design safe compile-time systems
- create maintainable abstractions
- build framework-level tooling
The concepts explored here are inspired by systems used in:
- Meta Platforms infrastructure tooling
- Google compiler systems
- Amazon backend infrastructure
- Microsoft systems engineering
as well as Rust ecosystem projects like:
- Serde
- Tokio
- Axum
- Actix Web
- SQLx
Additional notes and explanations can be found inside the docs/ directory.
Topics include:
- macro expansion
- token trees
- recursion
- hygiene
- procedural macro architecture
- DSL design patterns
Contributions are welcome.
To contribute:
- Fork the repository
- Create a feature branch
- Implement improvements
- Add documentation and tests
- Submit a pull request
This project is open-source and available under the MIT License.
ALX Rust Macro Engineering Lab
Focused on systems programming, metaprogramming, compiler-style engineering, and Rust infrastructure development.