How to make variable step iteration?
How to extend Iterator trait with custom method?
This solution should be without unnecessary performance penalties.
Desired example:
for i in (1..10).step_by_mul(2) {
println!("{}", i);
}
You used to be able to do this with std::iter::iterate or std::iter::Unfold, but these were never stabilized. I think they’re now removed altogether.
There is itertools::Unfold, but I don’t know if that has a convenient wrapper like we had in std::iter::iterate – @bluss?
There is the unstable std::iter::StepBy (and associated issue). Last word is that the API still needs some work, and so hasn’t entered its final comment period. If you want this to happen, it would be helpful to hear any opinions voiced in the issue thread.
I don’t think you are going to find anything “standard”. Stepping by an increasing value isn’t something that people need very often. Stepping by a fixed amount other than 1 is way more common, and I don’t think we even have that. You are going to have to run a while loop:
let i = 1
let step = 1
while i < 10 {
println!("{}", i);
i += step;
step *= 2;
}