Skip to content
tezvyn:
Menu

Implement a custom derive macro for a Builder pattern

Source: doc.rust-lang.orgHardHow cards are made

Tests proc-macro AST transformation. A strong answer lists: parse TokenStream with syn into DeriveInput, inspect fields, then quote builder code as TokenStream, noting the separate proc-macro crate. Red flag: treating tokens as strings instead of AST nodes.

What's really being asked

Your ability to reason about Rust compile-time metaprogramming. Specifically, the interviewer wants to know if you understand the boundary between the proc-macro crate and normal Rust code, how to inspect an AST without hand-rolling a parser, and how to emit hygienic code that integrates with the compiler.

The full answer

Four things in order. First, the macro entry point is a function annotated with proc_macro_derive that takes a TokenStream and returns a TokenStream. Second, you pass that input to syn to parse it into a DeriveInput, which lets you iterate over fields, types, and attributes. Third, you build the desired output, for example by creating a new builder struct with optional fields and setter methods, using quote to turn Rust-like syntax back into a TokenStream. Fourth, you note that proc macros must reside in their own crate with crate-type proc-macro, and that quote preserves hygiene so generated identifiers do not accidentally collide with user code.

The mistakes people make

Treating TokenStream as strings and concatenating text. This is a red flag because it ignores token hygiene and parsing edge cases. Another mistake is forgetting that derive macros only add code and cannot modify the original struct, or confusing declarative macros macro_rules with procedural macros. Some candidates also omit the separate crate requirement entirely.

What usually comes next

How would you handle generics and where clauses on the target struct. How would you support optional attributes on fields, such as builder default values or renaming. How do you report meaningful compile errors from inside a proc macro rather than panicking. What is the difference between quote and quote_spanned for error propagation.

A concrete example

Imagine a struct Command with fields executable and args. The derive macro would parse the struct, generate a CommandBuilder with private optional fields, implement methods like executable and args that return self, and a build method that returns Command after checking that required fields are present. The generated code is produced by quote and returned as a TokenStream.

Interview question

In a proc-macro derive that generates a Builder pattern, what is the primary role of the quote crate in the final step?

  • a.To convert a Rust-like template into a TokenStream while preserving hygiene for generated identifiersCorrect
  • b.To validate that the generated builder code compiles before returning it to the compiler
  • c.To provide procedural hooks that allow the macro to modify the original struct definition in place
  • d.To parse the input TokenStream into a typed AST representing the target struct
Why?

The card explains that quote turns Rust-like syntax back into a TokenStream and preserves hygiene so generated identifiers do not collide with user code. Option D describes syn's role, and D reflects the common misconception that derive macros can modify the original struct.

Just read this? Test yourself on what you have been reading.

Read the original → doc.rust-lang.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on rust — each one lists the topics its interview covers.

See open roles