this post was submitted on 10 Aug 2025
832 points (95.0% liked)

Programmer Humor

25671 readers
2288 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] dejected_warp_core@lemmy.world 2 points 1 day ago* (last edited 1 day ago)

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