简体   繁体   English

如何将字符串连接到宏派生中的标识?

[英]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.我需要创建一个宏派生,其中名称是 function 名称的一部分。 (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 :根据quote文档,您可以使用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>> {
       ...
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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