diff --git a/src/iter_index.rs b/src/iter_index.rs index fd945d652..679a88075 100644 --- a/src/iter_index.rs +++ b/src/iter_index.rs @@ -48,11 +48,11 @@ impl IteratorIndex for RangeInclusive where I: Iterator, { - type Output = Take>; + type Output = Skip>; fn index(self, iter: I) -> Self::Output { - iter.skip(*self.start()) - .take((1 + *self.end()).saturating_sub(*self.start())) + assert_ne!(*self.end(), usize::MAX); + iter.take(self.end() + 1).skip(*self.start()) } } @@ -74,6 +74,7 @@ where type Output = Take; fn index(self, iter: I) -> Self::Output { + assert_ne!(self.end, usize::MAX); iter.take(self.end + 1) } } diff --git a/src/lib.rs b/src/lib.rs index 80e1c47f4..b5fc258e0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -511,6 +511,8 @@ pub trait Itertools: Iterator { /// /// Works similarly to [`slice::get`](https://doc.rust-lang.org/std/primitive.slice.html#method.get). /// + /// **Panics** if the range includes `usize::MAX`. + /// /// It's a generalisation of [`Iterator::take`] and [`Iterator::skip`], /// and uses these under the hood. /// Therefore, the resulting iterator is [`DoubleEndedIterator`]