We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
std::iter::Skip::count
1 parent 900811e commit 9d3e844Copy full SHA for 9d3e844
src/libcore/iter/adapters/mod.rs
@@ -1815,8 +1815,14 @@ where
1815
}
1816
1817
#[inline]
1818
- fn count(self) -> usize {
1819
- self.iter.count().saturating_sub(self.n)
+ fn count(mut self) -> usize {
+ if self.n > 0 {
1820
+ // nth(n) skips n+1
1821
+ if self.iter.nth(self.n - 1).is_none() {
1822
+ return 0;
1823
+ }
1824
1825
+ self.iter.count()
1826
1827
1828
src/test/ui/iterators/skip-count-overflow.rs
@@ -0,0 +1,8 @@
1
+// run-pass
2
+// only-32bit too impatient for 2⁶⁴ items
3
+// compile-flags: -C overflow-checks -C opt-level=3
4
+
5
+fn main() {
6
+ let i = (0..usize::max_value()).chain(0..10).skip(usize::max_value());
7
+ assert_eq!(i.count(), 10);
8
+}
0 commit comments