Help learning proc macros

I saw this video, and thought that proc macros are petty cool. However, while this was a cool video, it was not the best tutorial on proc macros in general. Is there any good resource on learning proc macros from the ground up? I have some notion of how to make declarative macros, but as i understand it, proc macros are a different beast all together.

2 Likes

The little book of Rust macros is my go-to reference for macro-related questions:

3 Likes

Do you know of anything equivalent to rustlings for macros? as much as i can learn by reading, i find learning by doing is much easier for me

No, but a quick search revealed this freeCodeCamp tutorial:

which looks pretty beginner friendly and quite detailed, based on a quick glance.

1 Like

This repo was extremely helpful when I was trying to learn proc macros for a project.

It has several projects with tests you have to pass, and I haven't found any resource better after you get a basic idea of what a proc macro is. Highly recommended :slight_smile:

EDIT: There are solutions out there if you get stuck, sometimes it is helpful to check the solutions to understand how some of the more 'advanced' patterns work.

2 Likes

I had a short look -- close to the beginning we have:

Using macros, you can also dynamically add things that are required to be added at compilation time, which is not possible using functions since they get called at runtime. One such feature, for example, is implementing Traits on types, which is required to be implemented at compilation time.

I can not say that I like these two sentences that much -- but well I know not much about macros yet.

There's a little bit about procedural macros in the Book:

It's by no means in-depth, but it's quite useful for understanding a bit about them.