Live data from Hacker News

Let's write a macro in Rust

hackeryarn.com

11–20 of 50 posts

Re: Let's write a macro in Rust

#11
post #7
post #5

Having done some Rust macros, the entire thing is such a huge hidden wart. Even very simple meta-programming in Rust need to be in their separate crate and has to use very complex syntax and invocation. Also the whole "don't write macros" is such a hilarious statement given that entire Rust ecosystem is built on them.

Eh. I agree there's complexity in them, and that a lot of the ecosystem indirectly use them because of derive macros, but I wouldn't call it a wart, and the "syntax" for writing proc macros isn't that bad if you use the syn/quote crates. I agree that decl macros usually make me sigh and open the docs though

> I agree that decl macros usually make me sigh and open the docs though

Declarative macros are very like regex's: both in that at first they seem incredibly dense and arcane but once you work out how to read them they're actually very simple, and in that the syntax is literally similar in that it's a list of tokens that must match sequentially.

Re: Let's write a macro in Rust

#12
post #5

Having done some Rust macros, the entire thing is such a huge hidden wart. Even very simple meta-programming in Rust need to be in their separate crate and has to use very complex syntax and invocation. Also the whole "don't write macros" is such a hilarious statement given that entire Rust ecosystem is built on them.

Maybe "don't write your own macros". I guess it's like C++ template meta programming. If you find yourself using that a lot, it's probably a bad sign. But also Eigen is pretty great.

Re: Let's write a macro in Rust

#13

I like using or exposing macros when appropriate. They can simplify messy syntax, or prevent repetitions not allowed by the compiler directly. I can't get the syntax for writing them to stick. So, I am happy with letting LLMs handle this. Sometimes the macros are for large chunks of code that uses a different type in a key place, or sometimes they're for cleaning up verbose function calls that will be made multiple t…

> I can't get the syntax for writing them to stick

Oh man, same. I wrote a macro crate then went back to it months later and couldn't make heads nor tails of anything I wrote. Something about the syntax just doesn't stick in my brain.

Re: Let's write a macro in Rust

#14

Bjarne Stroustrup said "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off". The same is true of Rust macros. When you need them they're awesome, but you should almost never need them - add a new macro as a very last resort.

I agree with this, mostly because all the macros I would use are mostly written already. If const generic expressions arrive, a lot of those could go away too. In languages like lisp or zig where metaprogramming is a central feature, what do they do differently to make it better? Would those things have worked for rust?

Re: Let's write a macro in Rust

#19

Bjarne Stroustrup said "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off". The same is true of Rust macros. When you need them they're awesome, but you should almost never need them - add a new macro as a very last resort.

That's a hilarious quote. Thank you.

Re: Let's write a macro in Rust

#20
So, this article is writing a "by example" macro, and my impression is that many comments here are thinking about Rust's procedural or "proc" macros and most particularly its entirely general procedural macros which are indeed arbitrary code executing in the compiler. That's not what the article is about at all.

"by example" macros are actually just a fancier version of templating systems I'd expect most of you have used. Their biggest fancy feature is probably repetition, it's easy for Rust's macro to say if we've got a list of N things, we're going to emit this same code N times, but with each thing in turn filled out.

The syntax doesn't look that much like the rest of Rust but that's mostly an ergonomic consideration, so you can distinguish between your macro and the code your macro is spitting out. In fact "at least the syntax is the same" is one of the few ways the procedural macros are simpler, since they're literally some Rust run by your compiler.

Post reply on HN