Skip to content
0:00 / 0:00

#rust#proceduralmacros#syn

Procedural Macros in Rust (part 1)

Jon Gjengset

111K subscribers • 38 videos on Hallway

YouTube

Advanced. Deep dive for people who know the field.

80K views7 years ago8 recommendations

Programming languagesSystems programmingRustCompilers and interpreters

Jon Gjengset walks through David Tolnay's procedural macro workshop, implementing a custom derive macro from scratch.

In this stream, we take a look at Rust's procedural macros. These are much more flexible than the declarative macros you get with the macro_rules! construct, and effectively allow you to write source-code transformations as a separate program. Take a look at doc.rust-lang.org/book/ch19-06-macros.html and doc.rust-lang.org/nightly/reference/procedural-macros.html for details. In particular, we follow David Tolnay's excellent proc-macro workshop from Rust Latam back in March 2019: github.com/dtolnay/proc-macro-workshop. The goal is to implement the custom derive(Builder) project (the first one listed). We managed to get all the way through and then some! For tools, we ended up relying quite heavily on David's github.com/dtolnay/syn and github.com/dtolnay/quote, as well as the very handy cargo-expand tool: github.com/dtolnay/cargo-expand. You can find the code over at github.com/jonhoo/proc-macro-workshop. Oh, and the issue with formatting doc comments we encountered at the end has already been fixed @ github.com/rust-lang/rustfmt/pull/3548 ! You can see the live version with comments at youtube.com/watch?v=0-sMn2fXYG4

Why it's here

8 people recommend this

What people say. Highly recommended as a comprehensive, step-by-step guide to understanding and writing procedural macros in Rust.

u/420goonsquad420in r/rust • Recommends

On “Talk to me about macros”

…Rust proc macros are a whole different world. They let you execute arbitrary code at compile time, meaning you can make up syntax, run it through a proc macro, and have it spot out Rust code at the end. It's also how traits can be derived. Proc macros are magic. If you want to learn more, I'd personally recommend Jon Gjengset's video(s) on them: link to this video link to this video link to this video Note that this is collectively about 10 hours of video, so buckle in.…

u/WrongJudgment6in r/rust • Recommends

On “Where is the source / a *usable* reference/tutorial on metaprogramming?”

There's a tutorial from Jon Gjengset link to this video and The Little Book of Rust Macros

u/WhoisDavidin r/rust • Recommends

On “Procedural Macros: The Basics | blog.turbo.fish”

Also a great intro on this by /u/jonhoo : link to this video That inspired me to go through the workshop. I can definitely relate to the steep learning curve and lack of external resources. The one learning which kinda explains this is that proc macro in their current state are quite hacky and you do need to get over the hump, byte the bullet and be creative with rust! If interested here it is: https://github.com/WhoisDavid/rust-learnings/blob/master/proc-macro-workshop

u/1668553684in r/rust • Recommends

On “Compiling into rust followed by rust compiler?”

You could look into procedural macros, this seems like a possible solution. Basically, procedural macros are functions that run at compile time rather than runtime, which take token steams as input and produce Rust source code as output, which then get compiled into an executable by the Rust compiler. They're pretty complex, but it sounds like this is what you want to do. Here's a video series by Jon Gjengset covering David Tolnay's procedural macro workshop: link to this video (it's on the longer side, but it's very high quality).

u/trabe_lazyin r/rust • Recommends

On “Is there a simple proc-macro derive attributes example?”

https://github.com/dtolnay/proc-macro-workshop A pretty good workshop to implement some useful yet not too complicated proc macros. And you can watch Jon’s live coding for this workshop link to this video .

u/fear_the_morrokin r/rust • Recommends

On “Resources for writing good proc-macros?”

In addition to this, Jon Gjengsets accompanying videos, where he walks through some of the workshop exercises is very helpful. part 1 part 2

u/twe4kedin r/rust • Recommends

On “The Little Book of Rust Macros”

link to this video

u/MasterTextmanin r/rust • Recommends

On “Tablefy - my first crate!”

I would say that so far it was the hardest part of Rust I've touched, but even then it was still manageable! The most troublesome bit is that I couldn't find much helping material on how to make one, only to start one. What really helped were these two videos, where you see a procedural macro being built bit by bit. You can follow the workshop in the description and the video, and you'll get a pretty good gist of how to make one. It mostly consists of going through and forming new code from a destructured AST. This is all for *procedural macros,* there are other kinds that I haven't touched yet so far, but all in due time!