macro

Transform an abstract syntax tree

An abstract syntax tree lets a generator modify one syntactic property without manually searching a source string. This helper renames a parsed function and emits tokens with quote. A production transform must account for references to the original name. Example: external-dependency project; see fixtures/external-token-tools/src/bin/syntax_transform.rs. This caller is not a standalone rustc program. Expected stdout (not executed): 'new_name\n' Common mistake: Assuming a syntax-tree rename updates every semantic reference automatically.

Formal Syntax & Structure

rust syntaxrust
item.sig.ident = syn::Ident::new(...)

Working Example

Example Snippetrust
fn main() { let mut item: syn::ItemFn = syn::parse_str("fn old() {}").unwrap(); item.sig.ident = syn::Ident::new("new_name", proc_macro2::Span::call_site()); let tokens = quote::quote!(#item); let changed: syn::ItemFn = syn::parse2(tokens).unwrap(); println!("{}", changed.sig.ident); }
Rust Reference