Macro Plan
Thatβs an intense study session! π You can definitely get very comfortable with Rust macros in that time if you focus smartly. Here's a roadmap to maximize your learning:
Hour 1-2: Basics of Declarative Macros (macro_rules!
)
β
Understand how macro_rules!
works
β
Learn about patterns ($expr
, $ident
, $tt
, $block
)
β
Practice simple macros like println!
, vec!
, and custom ones
πΉ Exercises:
- Write a macro that squares a number
- Write a macro that repeats a string n
times
- Write a macro that creates a HashMap
π Reference:
- Rust Book - Macros
Hour 3-4: Intermediate Declarative Macros
β
Handle multiple arguments
β
Use recursion inside macros
β
Use tt
(token trees) for flexible macros
β
Debugging macros (macro_rules!
pitfalls)
πΉ Exercises:
- Write a min!
macro that finds the smallest of multiple numbers
- Write a log!
macro that prints with different log levels
π Reference:
- Rust Reference - Macros
Hour 5-6: Introduction to Procedural Macros
β
Learn about proc_macro
and proc_macro2
crates
β
Understand function-like, derive, and attribute macros
β
Write a simple derive macro (e.g., #[derive(Hello)]
)
πΉ Exercises:
- Create a custom derive macro that prints "Hello from struct!"
- Write a procedural macro that converts a function into an async version
π Reference:
- Rust Book - Procedural Macros
- Rust API Docs - proc_macro
Hour 7-8: Advanced Procedural Macros
β
Work with syn
and quote
crates
β
Parse Rust syntax trees
β
Implement attribute-like macros (#[my_macro]
)
πΉ Exercises:
- Implement a macro that adds logging to any function
- Write a macro that converts function names to uppercase
π Reference:
- Procedural Macros Workshop
Hour 9: Review, Experiment, and Write Your Own Macros
β
Revisit concepts
β
Implement your own creative macros
β
Debug and understand real-world macros (serde
, tokio::main
)
Tips for Staying Focused
π‘ Take short 5-minute breaks every hour
π‘ Code along with examples instead of just reading
π‘ Use cargo expand
to see how macros expand
If you stick to this plan, youβll get really comfortable with Rust macros. π₯ Want help with a specific macro concept while working through this?