Rust macros are definitely a thing
Conversation
The macro system is powerful and necessary. Rust generics are closer to templates but not overpowered and prone to abuse like C++
1
1
They're not really anything like templates. They work with syntax trees and are part of the parser. The comparison to make is with C macros, although they aren't like those legacy macros at all but rather more sophisticated and sane Lisp-style macros for making specialized DSLs.
1
1
The comparison for templates is with generics, which are implemented in a similar way but they require type class (trait) bounds specified for everything that's used, rather than expanding and pattern matching. There's not template meta-programming in the same sense as C++.
1
1
There are also procedural macros (compiler plugins), which are usable for some pretty cool things:
github.com/sfackler/rust-
docs.rs/regex/0.1.46/r
The standard library's type-checked format! macro is implemented as one of those:
doc.rust-lang.org/std/macro.form
1
This Tweet was deleted by the Tweet author. Learn more
Usage or implementation of macros? The usage is just macro!(tokens), and the syntax within it is determined by how the macro defines the DSL. Implementing macros is definitely not pretty for anything complex, as you're messing with a token tree rather than it being normal code.
1
1
If I remember correctly, the reason declarative macros are currently defined with macro_rules! is because there wasn't time to make a good implementation for 1.0 so that was kept as a compromise rather than not having it at all. The macro keyword is reserved for a replacement.
1
So, the usage of macros will remain the same as it is now, but macro_rules! is going to be replaced. Procedural macros are also still an experimental feature reserved for the compiler outside of nightlies but that's a really nice feature and just hasn't been standardized/frozen.
1
A small portion of procedural macros did get standardized for custom deriving implementations which is used by libraries like serde.rs and diesel.rs.
github.com/rust-lang/rfcs
Writing declarative macros is still the garbage legacy macro_rules! though.
I worked on Rust before 1.0 and leading up to it, and I remember clearly that there just wasn't time to make a good declarative macro implementation. It's too important to leave out, so it's designed in a way that's meant to be replaced with the superior keyword reserved for it.
1
The usage of them would remain the same though. It's the syntax for defining them that will be replaced. I think the syntax of macro!(tokens) is completely fine. Procedural macros are just Rust code so they don't have the same issue of needing special syntax designed for them.



