How to write a macro that repeats some code n times?

I'd like to write a macro that used like this:

zip_n_times!(sequence, 3);
zip_n_times!(sequence, 5);

produces an output like:

itertools::izip!(sequence.skip(0), sequence.skip(1), sequence.skip(2));
itertools::izip!(sequence.skip(0), sequence.skip(1), sequence.skip(2), sequence.skip(3), sequence.skip(4));

macro_rules! zip_n_times {
    ( $seq:expr, $n:literal ) => {

How can I implement the "for loop" that outputs a token (sequence.skip(n)) $n times?

I'm afraid this is impossible with declarative macros, since they don't afford any kind of (ordinary) computation.

With a small value of n, I think you can do it in a less desirable way like:

zip_n_times!(sequence, 0 1 2);
zip_n_times!(sequence, 0 1 2 3 4);

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.