Rust Macros Quiz

Rust
0 Passed
0% acceptance

Master Rust's metaprogramming system with declarative and procedural macros for code generation and compile-time programming

40 Questions
~80 minutes
1

Question 1

What is a macro in Rust?

A
Code that generates code at compile time
B
Runtime code execution
C
Function that returns values
D
Macros don't exist in Rust
2

Question 2

What is the difference between macros and functions?

A
Macros generate code, functions execute code; macros can take variable args
B
They are identical
C
Functions are more powerful
D
No difference
3

Question 3

What does macro_rules! define?

A
Declarative macro using pattern matching rules
B
Procedural macro
C
Function macro
D
macro_rules! doesn't exist
4

Question 4

What does this basic macro example do?

rust
macro_rules! say_hello {
    () => {
        println!("Hello!");
    };
}

say_hello!();
A
Expands to println!("Hello!") at compile time
B
Compilation error
C
Runtime error
D
Does nothing
5

Question 5

What are macro fragments (designators)?

A
Matchers like $x:expr, $y:ty for different code elements
B
Macro arguments
C
Macro output
D
Fragments don't exist
6

Question 6

What does this parameterized macro show?

rust
macro_rules! multiply {
    ($x:expr, $y:expr) => {
        $x * $y
    };
}

let result = multiply!(2 + 3, 4);
A
Expands to (2 + 3) * 4, result is 20
B
Compilation error
C
Expands to 2 + 3 * 4
D
Runtime error
7

Question 7

What is macro repetition?

A
Using $(...)* to repeat patterns zero or more times
B
Macro execution multiple times
C
Loop inside macro
D
Repetition doesn't exist
8

Question 8

What does this repetition macro do?

rust
macro_rules! vec_macro {
    ($($x:expr),*) => {
        {
            let mut temp_vec = Vec::new();
            $(temp_vec.push($x);)*
            temp_vec
        }
    };
}
A
Creates Vec from comma-separated expressions
B
Compilation error
C
Creates array
D
Runtime error
9

Question 9

What is macro hygiene?

A
Prevention of variable name conflicts between macro and call site
B
Macro cleanliness
C
Macro performance
D
Hygiene doesn't exist
10

Question 10

What is variable capture in macros?

A
When macro variables shadow or conflict with call site variables
B
Variable assignment
C
Variable declaration
D
Capture doesn't exist
11

Question 11

What does $crate mean in macros?

A
Refers to crate where macro is defined, hygiene bypass
B
Current crate
C
Macro crate
D
$crate doesn't exist
12

Question 12

What are procedural macros?

A
Macros that run Rust code to generate code, more powerful than declarative
B
Step-by-step macros
C
Function-like macros
D
Procedural macros don't exist
13

Question 13

What are the three types of procedural macros?

A
Custom derive, attribute-like, function-like
B
Declarative, procedural, functional
C
Simple, complex, advanced
D
No types exist
14

Question 14

What is a custom derive macro?

A
Generates impl blocks for traits based on struct/enum fields
B
Derives functions
C
Derives variables
D
Custom derive doesn't exist
15

Question 15

What is the syn crate used for?

A
Parsing Rust code into AST for procedural macros
B
Syntax highlighting
C
Code generation
D
syn doesn't exist
16

Question 16

What is quote crate used for?

A
Generating Rust code from AST back to TokenStream
B
Code quoting
C
String manipulation
D
quote doesn't exist
17

Question 17

What does this custom derive skeleton show?

rust
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()
}
A
Basic structure of custom derive macro using syn and quote
B
Compilation error
C
Runtime macro
D
Declarative macro
18

Question 18

What is an attribute-like macro?

A
#[my_macro] that can appear on items, generates or modifies code
B
Function attribute
C
Variable attribute
D
Attribute macros don't exist
19

Question 19

What is a function-like macro?

A
my_macro!(...) that looks like function call but generates code
B
Regular function
C
Method
D
Function-like macros don't exist
20

Question 20

What is TokenStream?

A
Sequence of tokens representing Rust source code
B
Data stream
C
Binary stream
D
TokenStream doesn't exist
21

Question 21

What is macro expansion?

A
Process where macros are replaced with generated code
B
Code execution
C
Compilation
D
Expansion doesn't exist
22

Question 22

How do you debug macro expansion?

A
Use cargo expand or intellij-rust's macro expansion
B
Use println! in macro
C
Use debugger
D
Cannot debug macros
23

Question 23

What is macro recursion?

A
Macro calling itself or other macros in expansion
B
Runtime recursion
C
Function recursion
D
Recursion doesn't exist in macros
24

Question 24

What is TT muncher pattern?

A
Recursive macro pattern that processes tokens one by one
B
Token eating
C
Pattern matching
D
TT muncher doesn't exist
25

Question 25

What does this TT muncher example show?

rust
macro_rules! my_vec {
    ($($x:expr),* $(,)?) => {
        {
            let mut temp = Vec::new();
            $(temp.push($x);)*
            temp
        }
    };
}
A
Handles optional trailing comma in macro arguments
B
Compilation error
C
Required comma
D
Runtime error
26

Question 26

What is macro shadowing?

A
Local macro definition shadows global one
B
Variable shadowing
C
Function overriding
D
Shadowing doesn't exist in macros
27

Question 27

What is the difference between macro_rules! and procedural macros?

A
macro_rules! is simpler but limited, procedural more powerful but complex
B
They are identical
C
macro_rules! is better
D
No difference
28

Question 28

What is the performance impact of macros?

A
Compile-time cost only, no runtime overhead
B
Runtime performance cost
C
Memory overhead
D
No performance impact
29

Question 29

What is macro export?

A
#[macro_export] makes macro available to other crates
B
Macro distribution
C
Macro printing
D
Export doesn't exist
30

Question 30

What is the problem with this macro?

rust
macro_rules! bad_macro {
    ($x:expr) => {
        let y = 2;
        $x + y
    };
}
A
Variable y may conflict with call site variables
B
Compilation error
C
Runtime error
D
No problem
31

Question 31

What is a common use case for macros?

A
DSL creation, reducing boilerplate, compile-time code generation
B
Runtime code execution
C
Dynamic typing
D
No common use cases
32

Question 32

What is the difference between macro and const fn?

A
Macros generate code at any position, const fn limited to values
B
They are identical
C
const fn is more powerful
D
No difference
33

Question 33

What is macro_rules_transducer?

A
Tool for debugging macro expansion step by step
B
Macro transformer
C
Code generator
D
Doesn't exist
34

Question 34

What is the recursion limit for macros?

A
Default 64, prevents infinite recursion
B
No limit
C
1000
D
Limit doesn't exist
35

Question 35

What is a macro anti-pattern?

A
Using macros when functions would work, overly complex macros
B
Simple macros
C
Well-documented macros
D
No anti-patterns
36

Question 36

What is the difference between local and global macros?

A
Local macros visible in scope, global exported with #[macro_export]
B
They are identical
C
Local are faster
D
No difference
37

Question 37

What is the problem with macros in expressions?

A
Macros can expand to multiple statements where single expression expected
B
Type checking
C
Performance
D
No problem
38

Question 38

What is declarative macro limitation?

A
Cannot inspect code structure, only pattern match syntax
B
Too powerful
C
Too slow
D
No limitations
39

Question 39

What is the future of macros in Rust?

A
Procedural macros becoming more important, better tooling
B
Macros being removed
C
No changes
D
Future unknown
40

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?

A
Custom derive for serialization, procedural macros for DSL, macro_rules! for simple repetition
B
Only functions
C
Only procedural macros
D
No macros needed

QUIZZES IN Rust