Macros
38 articles
Common Macro Patterns and Best Practices in Rust
Use declarative macros for repetition and procedural macros for custom derives to generate code at compile time.
Error: "macro expansion ignores token" — How to Fix
Fix the 'macro expansion ignores token' error by checking for syntax errors in your macro usage or definition.
Error: "no rules expected the token" in Macro — How to Fix
Fix the "no rules expected the token" macro error by ensuring your macro invocation matches the exact syntax pattern defined in the macro body.
How to debug macros
Debug Rust macros by using rustc flags or RUSTFLAGS to expand and inspect the generated code before compilation.
How to Debug Macros in Rust (cargo expand)
Use `cargo expand` to inspect the code generated by your macros before compilation, as this reveals the exact syntax the compiler sees and helps identify expansion errors.
How to Debug Proc Macro Compilation Errors
Use RUSTFLAGS="-Z macro-backtrace" to view expanded proc macro code and debug compilation errors.
How to Export Macros from a Crate in Rust
Add the #[macro_export] attribute to your macro_rules! definition to make it public to dependent crates.
How to Implement a Builder Pattern with Derive Macros
Implement a Builder pattern in Rust by adding the buildstructor crate and using the #[derive(Builder)] attribute on your struct.
How to Use cargo expand to See Macro Expansions
Use `cargo expand` to inspect the final Rust code after macro expansion by running it as a subcommand within your project directory.
How to Use cfg_attr to Conditionally Document Features
Use #[cfg_attr(feature = "name", doc = "text")] to add documentation only when a specific feature is enabled.
How to Use Common Built-in Macros (println!, vec!, todo!, etc.)
Use println! for output, vec! for lists, and todo! for placeholders to write concise Rust code.
How to Use Compile-Time Environment Variables in Rust (env! macro)
Use the env! macro to embed environment variable values as string literals at compile time in Rust.
How to use concat_idents macro
The concat_idents macro is removed; use the macro_metavar_expr_concat feature with ${concat(...)} syntax instead.
How to use include_str and include_bytes
Use include_str! for text and include_bytes! for binary data to embed files directly into your Rust binary at compile time.
How to Use Repetitions in Rust Macros ($(...),*)
Use $( ... )* or $( ... )+ in Rust macros to repeat token sequences zero or more times or one or more times respectively.
How to Use the cfg! and cfg_attr Macros for Conditional Compilation
Use #[cfg(test)] to include test modules and #[cfg_attr] to conditionally apply attributes like Debug during testing.
How to Use the compile_error! Macro for Better Error Messages
Use the compile_error! macro to halt compilation with a custom message when specific conditions are not met.
How to Use the concat!, stringify!, and env! Macros
Use concat!, stringify!, and env! macros to join literals, convert code to strings, and read environment variables at compile time.
How to Use the dbg! Macro for Quick Debugging
The `dbg!` macro is a built-in Rust tool that evaluates an expression, prints its value along with the source file and line number to stderr, and then returns the original value so execution continues uninterrupted.
How to Use the include_str! and include_bytes! Macros
Embed file contents directly into your Rust binary at compile time using include_str! for text and include_bytes! for raw data.
How to use the paste crate for macro tokens
Use the quote! macro to convert parsed tokens into a TokenStream for procedural macro generation.
How to Use the syn and quote Crates for Proc Macros
Use syn to parse Rust code into an AST and quote to generate new code from that structure for procedural macros.
How to Write a Function-Like Procedural Macro in Rust
Create a proc-macro crate with a #[proc_macro] function to generate code at compile time.
How to Write an Attribute Macro in Rust
An attribute macro in Rust is a procedural macro that transforms an item based on a custom attribute, defined using the proc_macro crate.
How to Write a Procedural Derive Macro in Rust
Create a library crate with the proc-macro type, define a function with the #[proc_macro_derive] attribute, and use syn and quote to generate the implementation code.
How to write attribute macros
Write attribute macros by defining a function with #[proc_macro_attribute] in a proc-macro crate to transform code at compile time.
How to write declarative macro
Define a declarative macro in Rust using the macro_rules! keyword to create reusable code patterns that expand at compile time.
How to Write Declarative Macros with macro_rules! in Rust
Define declarative macros in Rust using the macro_rules! construct to match code patterns and expand them at compile time.
How to write derive macro
Create a procedural macro crate with the `proc_macro_derive` attribute to automatically generate code for your types.
How to write procedural macro
Write a procedural macro by creating a library crate with `proc-macro = true` and implementing the `TokenStream` conversion logic to generate code at compile time.
What Are Macros in Rust and Why Are They Useful?
Macros in Rust are compile-time code generators that reduce boilerplate and enable flexible, reusable patterns.
What Is Macro Hygiene in Rust?
Macro hygiene in Rust prevents naming conflicts between macro-generated code and the surrounding scope by treating identifiers as unique.
What is the cfg attribute
The #[cfg(test)] attribute includes code only during test execution, excluding it from production builds.
What Is the dbg! Macro and How to Use It for Debugging?
The dbg! macro prints an expression's value and location to stderr, then returns the value unchanged for inline debugging.
What Is the Difference Between Declarative and Procedural Macros?
Declarative macros use `macro_rules!` to match patterns and expand code, while procedural macros are Rust functions that programmatically generate code for custom derives, attributes, or function-like calls. Declarative macros are defined with `macro_rules!` and work like pattern matching, whereas p
What is the difference between macro_rules and proc_macro
macro_rules! uses pattern matching for simple code generation, while proc_macro uses functions to manipulate the AST for complex derives and attributes.
What Is the matches! Macro in Rust?
The `matches!` macro is a built-in Rust utility that checks if a value matches a specific pattern, returning a boolean result without requiring the verbose syntax of a full `match` expression.
When to Use a Macro vs a Function vs a Trait in Rust
Use functions for logic, traits for shared behavior, and macros for code generation or variable arguments.