Start with the idea
It receives a TokenStream and returns one, running as a real program during compilation — which is why it lives in its own crate compiled for the host machine. That host requirement is why a constrained device-local toolchain may not support arbitrary procedural macro crates; cross-compilation normally runs the macro on the build host.
A procedural macro is a Rust program that runs while another crate is being compiled. The compiler gives it tokens; it parses them, generates new tokens, and returns them to the caller's compilation. Derive, attribute, and function-like procedural macros differ in invocation shape, but all need a host-executable macro crate. The expansion is then checked as ordinary Rust code. This makes proc macros powerful for repetitive APIs and also makes their diagnostics, build time, and dependency chain part of the developer experience.
The host/target distinction matters for cross-compilation.
Keep learning, for free
The rest of this lesson is waiting for you.
Create a free account to read the complete explanation, see the infographic, explore the code, and try the exercises. All 742 lessons and algorithm steps are included.
No payment details. Your account opens the complete Academy.