如何在 `quote!` 宏中的变量之后连接令牌?

How to concatenate token right after a variable within `quote!` macro?

use quote::quote;

fn main() {
    let name = "foo";
    let res = quote!(#name bar);
    println!("{:?}", res.to_string());
}

以上代码打印 "\"foo\" bar"。请在 Rust Playground 上尝试 运行 它 here

如何调整使变量部分和常量后缀成为单一标识?

我想在派生宏中使用 quote! 返回值。如何去掉双引号字符?

既然只需要引号bar,那么结合quote!format!的用法如何?

use quote::quote;

fn main() {
    let name = "foo";
    let res = format!("{} {}", name, quote!(bar));
    println!("{:?}", res.to_string());
}

playground.

如果您需要结果中的额外引号:

use quote::quote;

fn main() {
    let name = "foo";
    let res = format!("\"{}{}\"", name, quote!(bar));
    println!("{:?}", res.to_string());
}

playground.

我在这个 blog.

中找到了除了 Psidom 的答案之外我需要的解决方案的额外部分
use quote::quote;
use syn;

fn main() {
    let foo = "foo";
    let foobar = syn::Ident::new(&format!("{}bar", foo), syn::export::Span::call_site());
    let q = quote!(#foobar);
    println!("{}", q);
}

Playground.