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 49 of 100

What is a procedural macro in Rust, and how do you create one? Provide a simple example.?

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

In Rust, procedural macros are macros that are defined to generate and transform Rust code at compile-time. They are often used to generate code that is tedious or repetitive, or to enforce syntactic and semantic constraints on the code.

Creating a procedural macro involves defining a Rust macro that generates or transforms the Rust code according to the rules that you define. Procedural macros are implemented as Rust crates that are compiled as dynamic libraries and used as dependencies by other projects.

Here’s a simple example of a procedural macro that generates a function that adds two numbers:

    use proc_macro::TokenStream;
    use quote::quote;
    
    #[proc_macro]
    pub fn add_function(_: TokenStream) -> TokenStream {
        // Generate the Rust code for a function that adds two numbers
        let code = quote! {
            fn add_numbers(a: i32, b: i32) -> i32 {
                a + b
            }
        };
        
        // Parse the generated Rust code into a TokenStream object
        code.into()
    }

To use this macro, you would import the crate that contains it and call the ‘add_function‘ macro like this:

    fn main() {
        add_function!(); // Generates and compiles the `add_numbers` function
        let sum = add_numbers(3, 5); // Calls the generated function to add 3 and 5
        println!("Sum: {}", sum); // Prints "Sum: 8"
    }

In this example, the ‘quote‘ crate is used to generate the Rust code for the function, and the ‘into‘ method is used to convert the generated code into a valid ‘TokenStream‘ object that can be returned by the macro.

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