Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Syntactic macros and Procedural macros?

What is the difference between procedural macros and syntactic macros? Rust refers to its macro system as procedural, but I've seen language articles refer to a system like the rust macro system as syntactic macros. Syntactic macros would appear to have access to all or part of the AST when parsing them. Which appears to be what Rust has.

like image 844
perrymil Avatar asked Dec 12 '25 23:12

perrymil


1 Answers

Rust macros are syntactic; they work on the AST level.

What might be tripping you up in terminology is Rust has two flavors of macros that differ in how they are written and how they can be used. There are declarative macros (also called "macros by example") that are created by invocations of macro_rules!. And there are procedural macros, which are written as functions that handle TokenStreams as input and output (can be used as attributes, in derives, or like functions).

See also:

  • Macros in the Rust Book
  • Procedural Macros and Macros by Example in the Rust Reference
like image 72
kmdreko Avatar answered Dec 15 '25 12:12

kmdreko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!