DEV Community


The moment you write a macro. 💡

Adam Crockett
I work at ForgeRock as Staff UI Engineer, I play with all sorts really. Lately WASM is my toy of interest.
・2 min read

It's my second dip into Rust, the first time I got mad and rage quit a project because Rust is so damn good at stopping me from doing bad things.

This time away, must have let me absorb a large portion of the things I had read because for whatever reason, I'm not getting angry and actually it seems intuitive. 🤷‍♂️

Anyway macros, why I don't like them: abstraction, it's not something that helps me learn anything.

But that opinion changed a bit today I wrote a macro because I noticed something about Rust, a function with side effects is hard to write, functions seem to work best if they return a thing back to the "top level" and composition is key to this putting more complex things together, I found that I needed to write some really repetitive code several times and my code is starting to look hostile.

It turns out that macros seem to talk to the language as it parses, allowing you to dynamically insert templated pieces of code, a complex set of behaviour can be cut down to one line. I suppose it's a bit like code snippets in your IDE that expand into whatever, but you write this in the source of your project. I'm not sure the best way to store macros however, I really don't want this in my main module.

I think I want to use macros for code that is just painfully verbose and unwieldy but I will only when I have wrote the working version first, I doubt I will do everything in macros or start one from scratch.

Perhaps that's just where I am in my Rust journey.

Discussion (0)

Forem Open with the Forem app