I've got an array of 3 elements, and I want to create an iterator that cycles the elements a number of times. I tried the following code (included to show my intention):
let arr = [1, 2, 3]; iter::repeat(arr).take(10).flatten()
This doesn't work because
array does not implement
Iterator. I can't make an iterator by borrowing in a
flat_map because I create a temporary borrow that would have to live beyond the function. The following does work:
but this relies on the optimizer being able to propagate the constant nature of the length of &arr. Are there any better solutions?