WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Rust · Advanced · question 57 of 100

How do you create and use a custom derive macro in Rust? Provide an example.?

📕 Buy this interview preparation book: 100 Rust questions & answers — PDF + EPUB for $5

In Rust, custom derive macros allow you to write custom code that generates implementations of certain traits for your structs or enums. This can save you a lot of time and prevent you from writing repetitive code for implementing common traits such as Debug or Deserialize.

To create a custom derive macro, you first need to define a procedural macro crate that will contain your macro implementation. In this crate, you will implement the ‘proc_macro::derive‘ attribute macro. The ‘derive‘ function that you will implement takes a ‘TokenStream‘ (the input) and returns another ‘TokenStream‘ (the output).

Here’s an example of a custom derive macro that implements the ‘Description‘ trait for a struct:

    use proc_macro::TokenStream;
    use quote::quote;
    use syn::{parse_macro_input, DeriveInput};
    
    #[proc_macro_derive(Description)]
    pub fn derive_description(input: TokenStream) -> TokenStream {
        // Parse the input tokens into a syntax tree representation
        let input = parse_macro_input!(input as DeriveInput);
        
        // Extract the struct name and create an implementation of the Description trait
        let name = &input.ident;
        let output = quote! {
            impl Description for #name {
                fn describe(&self) -> String {
                    format!("This is a {} struct", stringify!(#name))
                }
            }
        };
        
        // Return the generated implementation as a TokenStream
        output.into()
    }

In this example, we define a ‘Description‘ trait that has a single method ‘describe()‘ that returns a string description of the struct it’s implemented on. Then, we use the ‘derive_description‘ macro to implement this trait for a given struct. The macro takes the input syntax tree representation of the struct, extracts its name using the ‘syn‘ crate, and generates the implementation of the ‘Description‘ trait using the ‘quote‘ crate.

Now, let’s use the macro we just defined on a struct:

    #[derive(Description)]
    struct MyStruct {
        field1: i32,
        field2: String,
    }

With this ‘derive‘ attribute, the ‘MyStruct‘ now has an implementation of the ‘Description‘ trait, so we can call its ‘describe()‘ method:

    let my_struct = MyStruct { field1: 42, field2: "hello".to_owned() };
    let description = my_struct.describe();
    println!("{}", description);
    // Output: "This is a MyStruct struct"

In summary, creating and using a custom derive macro in Rust involves:

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Rust interview — then scores it.
📞 Practice Rust — free 15 min
📕 Buy this interview preparation book: 100 Rust questions & answers — PDF + EPUB for $5

All 100 Rust questions · All topics