Live data from Hacker News

A Practical Intro to Macros in Rust

danielkeep.github.io

1–10 of 26 posts

Re: A Practical Intro to Macros in Rust

#3
Note that this dates back to Rust 1.0, and although the macro system hasn't changed since then, plans are in the works to succeed this original system ("macros 1.0") with an improved one ("macros 2.0"), culminating in the deprecation of the original. The two systems won't differ that much, so the documentation will remain mostly the same and the transition should be very straightforward.

The impetus for this is that the macro system was unanimously considered the least mature feature of pre-1.0 Rust, but there was little will to delay Rust 1.0 even further solely for the sake of macros. Instead, some expedient band-aids were slapped over it during the run-up to 1.0, with the intent to eventually fix the whole thing properly when time allowed.

To wit, the main problems with macros 1.0 were that macro "importing" is an ugly hack that stands out like a sore thumb from how modules work in the rest of the language; in addition, despite supposedly being hygenic, there are some edge cases where hygiene does not hold. The new system will make the language overall more consistent and reliable (and might bring some minor syntactic improvements), with the obvious downside that making the macro system nicer to use means that people might actually start using it. :P

Re: A Practical Intro to Macros in Rust

#5
The concept of a "token tree" as a separate stage between ordinary tokenization and full parsing seems interesting on its own: was it based on another system or invented for Rust? For example, you could imagine implementing incremental parsing by writing your parser as a transformation on a token tree.

Re: A Practical Intro to Macros in Rust

#6
post #4

The intro to macros starts with a recursive macro. Not a good approach for an intro. Recursion in macros is usually more of a toy than a useful tool, anyway.

No, it starts with a non recursive macro that does less stuff, and builds up to the recursive one.

This resource is a "learning by example" resource, and it teaches the macro bit by bit starting from a simplified version and building up to the recursive and complex part, teaching concepts along the way. That's a great way of structuring it.

Re: A Practical Intro to Macros in Rust

#7
post #4

The intro to macros starts with a recursive macro. Not a good approach for an intro. Recursion in macros is usually more of a toy than a useful tool, anyway.

No, it starts with a non recursive macro that does less stuff, and builds up to the recursive one. This resource is a "learning by example" resource, and it teaches the macro bit by bit starting from a simplified version and building up to the recursive and complex part, teaching concepts along the way. That's a great way of structuring it.

I actually think it's great that it starts by showing something that looks really impressive, it immediately made me think of list comprehensions in Haskell. I was very keen to keep reading as soon as I saw that.

Re: A Practical Intro to Macros in Rust

#9
The author also created The Little Book of Rust Macros[1], which is an invaluable resource. They also are one of the top answerers for Rust questions on Stack Overflow[2]. Altogether a great asset for the community!

[1]: https://danielkeep.github.io/tlborm/book/

[2]: https://stackoverflow.com/users/42353/dk?tab=answers

Re: A Practical Intro to Macros in Rust

#10
Hi. I'm a lead developer on the Shaka Scheme project that hopes to accomplish implementing Scheme R7RS, which also has hygienic declarative macros.

R7RS Scheme has `syntax-rules` which is very similar in structure to this macro system. It's quite elegant as it is, in my opinion, but I'm also wondering how good the debugging facilities for it are. Are they good?

Post reply on HN