Proc Macro Workshop: derive(Builder) [Part 1]

While continuing to learn a bit more about macros in Rust (previous post), I really want to move beyond the simple declarative macros and get into something a bit more interesting. Enter procedural macros. In a nutshell, procedural macros in Rust, rather than relying entirely on pattern matching and expansion are fully Rust functions.

They take a specific input (a stream of tokens) and output a specific output (a new stream of tokens), but in between they can do just about anything a full Rust function can do. And what’s better yet… they operate at compile time. And because they operate on tokens (rather than a full AST), you can do things that just aren’t syntactically valid in normal Rust. Things like… variadic functions (a la print! or var!) or even crazier things like embedding Python in Rust for … reasons.

Today specifically, I’ve started working through the prod-macro-workshop repo. It’s a series of five examples macros with test cases and some guidance set up to help you get up to speed. I’m going to be working through the first of these: derive(Builder). Now don’t get me wrong. I really have no idea what I’m doing, so don’t take this as an example of how to write a macro. But perhaps by writing this out, it will help me learn it better… and if you happen to learn something as well, all the better!

read more...


Writing a curry! macro for MacroKata

Recently I’ve been wanting to learn more about macros in Rust. It was always one of my favorite parts of Racket, so let’s see what we can do.

In order to do that, I’ve been following the excellent MacroKata series. It goes all the way through, starting with the very basics, adding in literals and expressions, handling repetition, nesting, and finally recursion.

What I really want to talk about those is the one that I found most interesting: curry!.

read more...