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.

Use syn to parse Rust code into an Abstract Syntax Tree (AST) and quote to generate new Rust code from that tree.

use proc_macro::TokenStream;
use quote::quote;

#[proc_macro_derive(HelloMacro)]
pub fn hello_macro_derive(input: TokenStream) -> TokenStream {
    let ast = syn::parse(input).unwrap();
    let name = &ast.ident;
    let generated = quote! {
        impl HelloMacro for #name {
            fn hello_macro() {
                println!("Hello, Macro! My name is {}!", stringify!(#name));
            }
        }
    };
    generated.into()
}