Is the custom derive example in the book useful?

Have you found value in the custom derive macro example in the book? Have you gone back to reference it? Or are other resources more helpful?

Thinking about cutting it, change my mind (or not ! :wink:)

Well, if I am to be brutally honest, most of the things in the "Advanced Features" chapter are not of "value", or at least of lesser value than the rest of the book.
But that's the point right? A little taste of intermediate stuff in an otherwise beginner book.
I'd vote for keeping it in.

2 Likes

Independent from the question of whether the custom derive example is necessary, when re-reading that page section I’ve just noticed that it didn’t mention one important difference between derive macros and attribute-like macros: Derive macros only generate additional code while leaving the original struct or enum unmodified, whereas attribute-like macros consume the item or expression they’re placed on and can return it in modified form.

3 Likes

Yes I have! When I just started learning about Rust macros.

No, but that's because I was already an experienced macrologist when I started learning about Rust macros.
So for me reading about Rust macros was mostly about the deltas relative to me previous knowledge, which was pretty much about the Rust-specific stuff.

This was most useful for learning about advanced techniques for declarative macros, eg TT-munchers.

1 Like

I could see myself recommending that chapter of the book to someone wanting to learn about proc macros.

That said, for anything more than a brief overview of how proc macros work my first preference would be Structuring, testing and debugging procedural macro crates from Ferrous Systems or David Tolnay's Procedural Macros Workshop. Both resources go through the full development process and cover a lot of the real world details the book skims over (e.g. testing and error handling).

1 Like

Note this version is old and unmaintained. There is an updated (+ new chapters) and maintained version here.

2 Likes

I'm a long-time Rustacean now starting to write a procedural macro for the first time. I've found some value in Chapter 19.5 "Macros",[1] in seeing the basic structure of such a project[2] but also, perhaps more importantly, having the subject presented as approachable (vs scary) simply by its being in the Book.

If this content is removed from the Book, where in Rust's documentation will it be documented? Only in the Reference? My impression of something that's documented only in the Reference or the 'Nomicon is that it's for wizards. I'm maybe experienced enough now to be okay using features that are documented only in the Reference (but not the 'Nomicon), but I imagine procedural macros nevertheless would feel less approachable and somewhat more "scary" if they weren't in the Book. I don't know whether that would be considered undesirable, although I guess that it would.


  1. although I'm writing an attribute macro, not a derive macro ↩︎

  2. such as [lib] proc-macro = true in Cargo.toml ↩︎

1 Like

Thank you for your input, everyone! I've decided to keep the proc macro custom derive example :slight_smile: You all convinced me!

5 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.