4 ms·
Regular Rust macros are hygienic, unlike macros in many (but definitely not all) other languages. This means that a macro expands in place in the AST, and can
by andrewaylett 5y ago
Regular Rust macros are hygienic, unlike macros in many (but definitely not all) other languages. This means that a macro expands in place in the AST, and can only operate on its inputs. The C preprocessor, by comparison, operates on text and the expanded macro can trivially interact with syntactic elements around it once expanded.
Procedural macros can run arbitrary rust code and can therefore do anything, but in general they don't -- and if you work within the interface you're given then the result will be hygienic.
There's a sense in which macro hygiene is analogous to Rust's safety guarantees -- the compiler will normally guarantee the abstraction, but there's an escape hatch where we rely on the person implementing the code that uses unsafe or implementing a procedural macro to not break the abstraction for their callers. A leaky abstraction is considered to be a bug.