【发布时间】:2021-03-28 21:33:17
【问题描述】:
我遇到了这个错误,并认为我非常hacky的解决方法可能对某人有用。
假设我有一些代码(playground),看起来像这样:
#[macro_use]
extern crate quote;
extern crate syn;
extern crate proc_macro2; // 1.0.24
fn main() {
let x = vec![
quote! {let x = 1;},
quote! {let x = 2;}
];
println!("{:#?}", quote! {
#x
});
}
这不会编译。
Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `Vec<TokenStream2>: ToTokens` is not satisfied
--> src/main.rs:11:23
|
11 | println!("{:#?}", quote! {
| _______________________^
12 | | #x
13 | | });
| |_____^ the trait `ToTokens` is not implemented for `Vec<TokenStream2>`
|
= note: required by `to_tokens`
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to previous error
如何使用quote! 连接Vec<proc_macro2::TokenStream>?
【问题讨论】:
标签: rust