@@ -106,9 +106,15 @@ pub trait Iterator {
106106
107107 /// Counts the number of elements in this iterator.
108108 ///
109+ /// # Undefined overflow
110+ ///
111+ /// The method does no guarding against overflows, so counting elements of
112+ /// an iterator with more than `usize::MAX` elements is undefined.
113+ ///
109114 /// # Panics
110115 ///
111- /// Panics if the number of elements overflows a `usize`.
116+ /// This functions might panic if the iterator has more than `usize::MAX`
117+ /// elements.
112118 ///
113119 /// # Examples
114120 ///
@@ -119,10 +125,8 @@ pub trait Iterator {
119125 #[ inline]
120126 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
121127 fn count ( self ) -> usize where Self : Sized {
122- self . fold ( 0 , |cnt, _| match cnt. checked_add ( 1 ) {
123- Some ( c) => c,
124- None => panic ! ( "overflow while counting the elements of an iterator" ) ,
125- } )
128+ // Might overflow.
129+ self . fold ( 0 , |cnt, _| cnt + 1 )
126130 }
127131
128132 /// Loops through the entire iterator, returning the last element.
@@ -156,10 +160,8 @@ pub trait Iterator {
156160 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
157161 fn nth ( & mut self , mut n : usize ) -> Option < Self :: Item > where Self : Sized {
158162 for x in self . by_ref ( ) {
159- n = match n. checked_sub ( 1 ) {
160- Some ( nn) => nn,
161- None => return Some ( x) ,
162- }
163+ if n == 0 { return Some ( x) }
164+ n -= 1 ;
163165 }
164166 None
165167 }
@@ -693,10 +695,16 @@ pub trait Iterator {
693695 ///
694696 /// Does not consume the iterator past the first found element.
695697 ///
698+ /// # Undefined overflow
699+ ///
700+ /// The method does no guarding against overflows, so when there are more
701+ /// than `usize::MAX` non-matching elements, the overflow behaviour is
702+ /// undefined.
703+ ///
696704 /// # Panics
697705 ///
698- /// Panics if the number of elements overflows a `usize` and no matching
699- /// element was found until that point .
706+ /// This functions might panic if the iterator has more than `usize::MAX`
707+ /// non-matching elements .
700708 ///
701709 /// # Examples
702710 ///
@@ -712,15 +720,11 @@ pub trait Iterator {
712720 Self : Sized ,
713721 P : FnMut ( Self :: Item ) -> bool ,
714722 {
715- let mut i = 0 ;
716- for x in self . by_ref ( ) {
723+ // `enumerate` might overflow.
724+ for ( i , x ) in self . by_ref ( ) . enumerate ( ) {
717725 if predicate ( x) {
718726 return Some ( i) ;
719727 }
720- i = match i. checked_add ( 1 ) {
721- Some ( ii) => ii,
722- None => panic ! ( "overflow while getting the position of an element in an iterator" ) ,
723- }
724728 }
725729 None
726730 }
@@ -1797,7 +1801,6 @@ pub struct Enumerate<I> {
17971801impl < I > Iterator for Enumerate < I > where I : Iterator {
17981802 type Item = ( usize , <I as Iterator >:: Item ) ;
17991803
1800-
18011804 /// # Undefined overflow
18021805 ///
18031806 /// The method does no guarding against overflows, so enumerating more than
0 commit comments