简体   繁体   中英

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

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?

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>> {
       ...
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM