How can I concatenate a string to an ident in a macro derive?

Viewed 2165

I need to create a macro derive where the name is part of the function name. (This code does not work, it is only to show the problem)

fn impl_logic(ast: &syn::DeriveInput) -> TokenStream {
    let name:&syn::Ident = &ast.ident;

    let gen = quote! {
       pub fn #name_logic() -> Arc<Mutex<UiAplicacion>> {
           ...
       }
    };

    gen.into()
}

How can I do this?

1 Answers

Based on quote's docs, you can construct a new identifier with syn::Ident:

let fname = format!("{}_logic", name);
let varname = syn::Ident::new(&fname, ident.span());

and then interpolate it:

let gen = quote! {
   pub fn #varname() -> Arc<Mutex<UiAplicacion>> {
       ...
   }
Related