Search code examples
rustrust-macros

"found struct `ThereIsNoIteratorInRepetition`" when trying to repeat over a vector using `quote!`


I'm trying to create a Vec of TokenStreams, and then use that list in another quote! macro:

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

    let generated = quote! {
        fn hello_world() {
            #(list);*
        }
    };

However, when compiling this, I'm getting this error:

expected struct `HasIterator`, found struct `ThereIsNoIteratorInRepetition`

From the macro's documentation, it seems that TokenStream should be valid in an interpolation, since it implements the ToTokens trait. Also, the list is a Vec, which is also explicitly allowed to be used in the loop interpolation.

Why am I getting the ThereIsNoIteratorInRepetition error when I am clearly using a valid iterator?


Solution

  • #(list);*
    

    Should be

    #(#list);*
    

    I missed the inner interpolation # in the repetition interpolation, and it was driving me crazy for hours. Leaving this here in case anybody runs into the same thing.

    I guess ThereIsNoIteratorInRepetition means that no interpolation was found in the repetition, when I originally thought it meant that the interpolation was parsed correctly, but was not accepted as an iterator.