diff --git a/library/core/src/iter/range.rs b/library/core/src/iter/range.rs index 44feb0a5638e5..391e03636abcd 100644 --- a/library/core/src/iter/range.rs +++ b/library/core/src/iter/range.rs @@ -765,6 +765,15 @@ impl Iterator for ops::Range { } } + #[inline] + fn count(self) -> usize { + if self.start < self.end { + Step::steps_between(&self.start, &self.end).expect("count overflowed usize") + } else { + 0 + } + } + #[inline] fn nth(&mut self, n: usize) -> Option { self.spec_nth(n) @@ -1162,6 +1171,17 @@ impl Iterator for ops::RangeInclusive { } } + #[inline] + fn count(self) -> usize { + if self.is_empty() { + return 0; + } + + Step::steps_between(&self.start, &self.end) + .and_then(|steps| steps.checked_add(1)) + .expect("count overflowed usize") + } + #[inline] fn nth(&mut self, n: usize) -> Option { if self.is_empty() {