当尝试使用“quote!”重复向量时,“找到结构‘ThereIsNoIteratorInRepetition’”

Rei*_*iks 8 rust rust-macros

我正在尝试创建一个Vecof TokenStreams,然后在另一个quote!宏中使用该列表:

    let list: Vec<_> = some_data
        .iter()
        .map(
            |item| {
                quote!{/*...*/}
            },
        )
        .collect();

    let generated = quote! {
        fn hello_world() {
            #(list);*
        }
    };
Run Code Online (Sandbox Code Playgroud)

但是,在编译时,我收到此错误:

expected struct `HasIterator`, found struct `ThereIsNoIteratorInRepetition`
Run Code Online (Sandbox Code Playgroud)

宏的文档来看,它似乎TokenStream在插值中应该有效,因为它实现了该ToTokens特征。此外,该列表是 a Vec,它也明确允许在循环插值中使用。

ThereIsNoIteratorInRepetition当我明确使用有效的迭代器时,为什么会收到错误?

Rei*_*iks 12

#(list);*
Run Code Online (Sandbox Code Playgroud)

应该

#(#list);*
Run Code Online (Sandbox Code Playgroud)

#我错过了重复插值中的内部插值,这让我发疯了好几个小时。把这个留在这里以防有人遇到同样的事情。

我想ThereIsNoIteratorInRepetition这意味着在重复中没有找到插值,当我最初认为这意味着插值被正确解析,但不被接受为迭代器时。