Write a procedural derive macro by creating a library crate with the proc-macro crate type, defining a function annotated with #[proc_macro_derive], and implementing the logic to parse and generate code using syn and quote.
// Cargo.toml
[lib]
proc-macro = true
[dependencies]
syn = "2.0"
quote = "1.0"
// src/lib.rs
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};
#[proc_macro_derive(MyTrait)]
pub fn my_trait_derive(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = &input.ident;
let expanded = quote! {
impl MyTrait for #name {}
};
TokenStream::from(expanded)
}
- Create a new library crate with
cargo new --lib my_derive. - Set
proc-macro = truein the[lib]section ofCargo.toml. - Add
synandquoteas dependencies inCargo.toml. - Import
TokenStream,quote, andsyntypes insrc/lib.rs. - Define a function annotated with
#[proc_macro_derive(YourTraitName)]. - Parse the input token stream into a
DeriveInputstruct usingparse_macro_input!. - Extract the struct or enum name from the parsed input.
- Use
quote!to generate the implementation code for your trait. - Return the generated code as a
TokenStream.