BorrowCheck › Rust language core
Macros
macro_rules!, hygiene, repetitions, procedural macros, derive and attributes.
The topic explains declarative macro_rules! macros: how rules are matched in order, what the expr, ty, tt, ident and pat fragments accept, why a substituted expr behaves as a single expression, and which tokens may follow a fragment (the follow-set). The subtopic on hygiene covers where names in a macro body are resolved, why local variables and loop labels do not mix with the caller's code, what $crate is for, and how a macro becomes visible outside its module. The questions on repetitions check separators, the *, + and ? operators, nested repetitions, recursive token munching, counting arguments, and the expansion recursion limit. The subtopic on procedural macros explains why they live in a separate crate with proc-macro = true, how they receive a TokenStream, parse it with syn, generate code with quote!, and report errors through compile_error!. The last subtopic is about derive and built-in macros: which bounds derive adds, what it can be applied to, how derive(Default) works on an enum, what stringify! and line! return, and why a format string is checked at build time.
44 questions: 18 basic, 22 intermediate, 4 advanced. 5 info pages, 31 sources.
Subtopics
macro_rules!and fragment specifiers- Hygiene
- Repetitions and recursion
- Procedural macros
derive, attributes and built-in macros
Previous: Unsafe, UB, FFI · Next: Testing, benchmarks, profiling