There are two types of macros in Rust:

Declarative macros: It's similar to a match expression that operates codes users provide as arguments.
Procedural macros: operates the AST of codes given.

Declarative macros

typesofmacrosinrustopen in new window

macro_rules! add {
    // match add!(1, 2)
    // match add!(1)

add!(1, 2);

Procedural macros

There are three subtypes of Procedural macros:

Derive macros: Derive Trait for struct, enum, union
Attribute-like macros: Attach attributes to an item and manipulates the item
Function-like macros: Very similar to Declarative macros, looks like function calls. Be executed at compile time instead of runtime
pub fn derive_trait(input: proc_macro::TokenStream) -> proc_macro::TokenStream {

struct SomeT {


pub fn some_attr(args: TokenStream, input: TokenStream) -> TokenStream {

fn do_something() {}

pub fn some_func(input: TokenStream) -> TokenStream {

some_func! { n in 0..10 {
    /* dome something */