Why does Range::step_by consume self?

I'm iterating over ranges (yes, you can guess i'm doing AOC2022 Day4) and while I completed it fine, it'd be nice to figure out if I could have avoided some silly things.

I expected (and maybe just missed?) some form of .iter() on a range. I only found step_by(usize) which is fine, but takes self (i expected &self). Am I missing something obvious? I specifically wanted an iter() to use any(FnMut).

Ranges do have .iter() but in different name. It's called .clone()


Range does not have .iter() because it itself is an Iterator. So you can directly use range.any(...)


Or all for part 1, respectively. In that case it might be necessary to actually call .clone() to not exhaust the range.

Indeed. That's design decision which confuses many newbies and IMNSHO one of few obvious design mistakes in Rust, but changing it now would definitely just create more confusion.

Because it's not even entirely clear whether having ranges as separate things from iterators and implementing into_iter for them would even be better 100% of time.

Most likely this would just cause different type of confusion.

1 Like

This whole discussion has turned into a real faceplam for me. It makes perfect sense that ranges are already iterators. Thanks everyone.

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.