Earlier quoted context omitted.
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.
Let's write a macro in Rust
31–40 of 50 posts
Re: Let's write a macro in Rust
#32Re: Let's write a macro in Rust
#33Yeah, Rust’s macro system feels like a double-edged sword — amazing power, but with a big learning curve. The tooling and syntax definitely make simple meta-programming harder than it should be.
Re: Let's write a macro in Rust
#34Bjarne 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.
can't follow
Re: Let's write a macro in Rust
#35One neat thing about the referenced chapter on macros from Practical Common Lisp is that it's chapters 7 and 8 of a 30+ chapter book. You're only learning about variables in the chapter before and then macros. I like to think about that when conversations about macros always devolve into talking about how you should never use macros. Now apologies for the aside—and I know I'm likely wading into a very expansive topic…
When to use them is a whole different story. But examples of macros I like are `when` and `unless`. Yes, simple, but they show a nice example of their power.
For more complicated once, love it or hate it, but the `loop` macro is probably THE prime example of a powerful macro.
Re: Let's write a macro in Rust
#36For those interested, this blog post also has a part 2 and 3: https://hackeryarn.com/post/rust-macros-2/ https://hackeryarn.com/post/rust-macros-3/
Re: Let's write a macro in Rust
#37One neat thing about the referenced chapter on macros from Practical Common Lisp is that it's chapters 7 and 8 of a 30+ chapter book. You're only learning about variables in the chapter before and then macros. I like to think about that when conversations about macros always devolve into talking about how you should never use macros. Now apologies for the aside—and I know I'm likely wading into a very expansive topic…
Because to_string allocates. And if a function requires a String (owned), it cannot accept a str reference (borrowed), it would defeat the purpose of the strong type system. String is moved, while str is passed by reference. There is the exception of Deref. If the function requires type A, and you pass it type B, which Derefs into type A, the compiler will Deref it for you. But that is zero cost and panic free, where…
Re: Let's write a macro in Rust
#38True, macros in Rust can feel heavy, but they also enable some powerful abstractions you just can’t get otherwise. I guess the “don’t write macros” advice is more about avoiding overuse than rejecting them completely.
Re: Let's write a macro in Rust
#39Having 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
#40One neat thing about the referenced chapter on macros from Practical Common Lisp is that it's chapters 7 and 8 of a 30+ chapter book. You're only learning about variables in the chapter before and then macros. I like to think about that when conversations about macros always devolve into talking about how you should never use macros. Now apologies for the aside—and I know I'm likely wading into a very expansive topic…
It’s shorter to write and takes up a little less space on screen to. I almost always use .into() when like in your example I initialize String field members of a struct in Rust.