#032
Mar 2026
32. iter::successors
Need to generate a sequence where each element depends on the previous one? std::iter::successors turns any “next from previous” logic into a lazy iterator.
| |
You give it a starting value and a closure that computes the next element from a reference to the current one. Return None to stop — here checked_mul naturally returns None on overflow, so the iterator terminates on its own.
It works great for any recurrence:
| |
Think of it as unfold for when your state is the yielded value. Simple, lazy, and zero-allocation until you collect.