[DSP] Rust macros love
by Paweł Świątkowski
21 Mar 2017
When I evaluated Rust as a language of choice, I didn’t really look into its macro capabilities. And that’s a shame, because it’s really worth mentioning.
Macros, for me, are a double-bladed sword - they bring a lot of power to the language, but can also render your code completely unreadable. The example of first one is Elixir, where you can do crazy things with macros that make coding much more pleasant. “Bad macros” are in C++ or VB.
In Rust, they can save you a lot of writing.
I met them while I pulled
clap library into my project. It’s a cargo package for handling command line options for your application. It’s similar to many tools available in other technologies in terms of logic. But take this snippet from my current code:
This is example of config using macros. It’s counterpart with “true” Rust code would be:
So, the “full code” is not so bad, however I think that it’s pretty obvious that macro version is much more concise and readable. And performance should be just the same.
How to write macros? For now it’s a bit too difficult for me. Maybe later. But as everything with Rust, there is an excellent tutorial available.