Rust Macros Quiz
Master Rust's metaprogramming system with declarative and procedural macros for code generation and compile-time programming
Question 1
What is a macro in Rust?
Question 2
What is the difference between macros and functions?
Question 3
What does macro_rules! define?
Question 4
What does this basic macro example do?
macro_rules! say_hello {
() => {
println!("Hello!");
};
}
say_hello!();Question 5
What are macro fragments (designators)?
Question 6
What does this parameterized macro show?
macro_rules! multiply {
($x:expr, $y:expr) => {
$x * $y
};
}
let result = multiply!(2 + 3, 4);Question 7
What is macro repetition?
Question 8
What does this repetition macro do?
macro_rules! vec_macro {
($($x:expr),*) => {
{
let mut temp_vec = Vec::new();
$(temp_vec.push($x);)*
temp_vec
}
};
}Question 9
What is macro hygiene?
Question 10
What is variable capture in macros?
Question 11
What does $crate mean in macros?
Question 12
What are procedural macros?
Question 13
What are the three types of procedural macros?
Question 14
What is a custom derive macro?
Question 15
What is the syn crate used for?
Question 16
What is quote crate used for?
Question 17
What does this custom derive skeleton show?
use proc_macro::TokenStream;
use syn::{parse_macro_input, DeriveInput};
use quote::quote;
#[proc_macro_derive(MyTrait)]
pub fn my_trait_derive(input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as DeriveInput);
let name = &ast.ident;
let gen = quote! {
impl MyTrait for #name {
// implementation
}
};
gen.into()
}Question 18
What is an attribute-like macro?
Question 19
What is a function-like macro?
Question 20
What is TokenStream?
Question 21
What is macro expansion?
Question 22
How do you debug macro expansion?
Question 23
What is macro recursion?
Question 24
What is TT muncher pattern?
Question 25
What does this TT muncher example show?
macro_rules! my_vec {
($($x:expr),* $(,)?) => {
{
let mut temp = Vec::new();
$(temp.push($x);)*
temp
}
};
}Question 26
What is macro shadowing?
Question 27
What is the difference between macro_rules! and procedural macros?
Question 28
What is the performance impact of macros?
Question 29
What is macro export?
Question 30
What is the problem with this macro?
macro_rules! bad_macro {
($x:expr) => {
let y = 2;
$x + y
};
}Question 31
What is a common use case for macros?
Question 32
What is the difference between macro and const fn?
Question 33
What is macro_rules_transducer?
Question 34
What is the recursion limit for macros?
Question 35
What is a macro anti-pattern?
Question 36
What is the difference between local and global macros?
Question 37
What is the problem with macros in expressions?
Question 38
What is declarative macro limitation?
Question 39
What is the future of macros in Rust?
Question 40
In a project needing to generate repetitive code for database models, create a DSL for configuration, and implement custom serialization logic, which macro approaches would you choose and why?
