Kind of. They do center on code generation, at the end of the day. That’s where the similarities end. You can’t insert macros into your code arbitrarily, nor can you generate arbitrary text as an output. Rust macros take parsed tokens as input, and generated (valid) code as output. They must also be used as annotations or similar to function calls, depending on how they’re written. The limitations can be frustrating at times, but you also never have to deal with brain-breaking #define shenanigans either.
That said, I’ve seen some brilliant stuff. A useful pattern is to have a macro span a swath of code, where the macro adds new/additional capabilities to vanilla Rust code. For example, here’s a parser expression grammar (PEG) implemented that way: https://github.com/kevinmehall/rust-peg
Kind of. They do center on code generation, at the end of the day. That’s where the similarities end. You can’t insert macros into your code arbitrarily, nor can you generate arbitrary text as an output. Rust macros take parsed tokens as input, and generated (valid) code as output. They must also be used as annotations or similar to function calls, depending on how they’re written. The limitations can be frustrating at times, but you also never have to deal with brain-breaking
#define
shenanigans either.That said, I’ve seen some brilliant stuff. A useful pattern is to have a macro span a swath of code, where the macro adds new/additional capabilities to vanilla Rust code. For example, here’s a parser expression grammar (PEG) implemented that way: https://github.com/kevinmehall/rust-peg