Use declarative macros for repetition and procedural macros for custom derives to generate code at compile time.
Use declarative macros with macro_rules! for code repetition and procedural macros with #[derive] or #[proc_macro] for custom attributes and derives. Declarative macros expand at compile time using pattern matching, while procedural macros are Rust functions that generate code.
macro_rules! say_hello {
() => {
println!("Hello!");
};
}
say_hello!();
Macros are like templates that write code for you before your program runs. You use them to avoid repeating yourself or to create new syntax that fits your specific needs. Think of them as a shortcut that automatically generates the boilerplate code you would otherwise have to type out manually.