Macros in Rust, the wrong way

submited by
Style Pass
2024-10-18 20:30:05

Your friends with strange hair colors and multiple partners keep telling you about this new language called Rust, which seems to be kinda like C++ but without all of the footguns you’ve been dealing with for years. You nervously dip what’s left of your toe in. While it’s strange at first, you quickly grow to enjoy it for all sorts of reasons — but try as you might, you just can’t quite let go of your old C-style macros.

Perfect, it looks like it might work. And hey, good job remembering to use -P to disable the line markers, the Rust compiler won’t like those at all. Speaking of the Rust compiler, the de-facto Rust build system Cargo expects your source to exist on disk, so for simplicity’s sake you invoke the compiler directly, using - to read the source code from stdin:

Ah, nuts. The leading hash has syntactic meaning in Rust, too, and the preprocessor doesn’t know what to do. That’s gonna be a problem.

Leave a Comment