Iterator::advance_by optimisations in std

Hello,

I have questions regarding the std's implementation of "advancing" the iterators: slice::Iter(Mut), iter::{Once, Repeat, Empty, FromFn}.

Looking at slice::Iter' Iterator::nth implementation you can see it does pointer arithmetics (post_inc_start) instead of looping, which is great. But it keeps the default advance_by (which is used by the default nth btw). Why?

For the "special" iterator in the iter module we do not get this optimization, we get the default looping behavior:

  • Once / Empty: I guess it is optimized away by the compiler here
  • Repeat / FromFn: are there used cases where we want to clone unused elements?

Just wondering why not writing the simple, obvious overrides.
Thanks!

My guess is that it is a newer API than nth and it wasn't re-implemented for all the stdlib iterators

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.