@@ -481,13 +481,11 @@ pub trait Iterator<A> {
481481 /// ```
482482 #[ inline]
483483 fn nth ( & mut self , mut n : uint ) -> Option < A > {
484- loop {
485- match self . next ( ) {
486- Some ( x) => if n == 0 { return Some ( x) } ,
487- None => return None
488- }
484+ for x in * self {
485+ if n == 0 { return Some ( x) }
489486 n -= 1 ;
490487 }
488+ None
491489 }
492490
493491 /// Loops through the entire iterator, returning the last element of the
@@ -518,11 +516,8 @@ pub trait Iterator<A> {
518516 #[ inline]
519517 fn fold < B > ( & mut self , init : B , f : |B , A | -> B ) -> B {
520518 let mut accum = init;
521- loop {
522- match self . next ( ) {
523- Some ( x) => { accum = f ( accum, x) ; }
524- None => { break ; }
525- }
519+ for x in * self {
520+ accum = f ( accum, x) ;
526521 }
527522 accum
528523 }
@@ -720,21 +715,10 @@ pub trait ExactSize<A> : DoubleEndedIterator<A> {
720715 /// If no element matches, None is returned.
721716 #[ inline]
722717 fn rposition ( & mut self , predicate: |A | -> bool) -> Option < uint > {
723- let ( lower, upper) = self . size_hint ( ) ;
724- assert ! ( upper == Some ( lower) ) ;
725- let mut i = lower;
726- loop {
727- match self . next_back ( ) {
728- None => break ,
729- Some ( x) => {
730- i = match i. checked_sub ( & 1 ) {
731- Some ( x) => x,
732- None => fail ! ( "rposition: incorrect ExactSize" )
733- } ;
734- if predicate ( x) {
735- return Some ( i)
736- }
737- }
718+ let len = self . len ( ) ;
719+ for i in range ( 0 , len) . rev ( ) {
720+ if predicate ( self . next_back ( ) . expect ( "rposition: incorrect ExactSize" ) ) {
721+ return Some ( i) ;
738722 }
739723 }
740724 None
@@ -744,7 +728,7 @@ pub trait ExactSize<A> : DoubleEndedIterator<A> {
744728 /// Return the exact length of the iterator.
745729 fn len ( & self ) -> uint {
746730 let ( lower, upper) = self . size_hint ( ) ;
747- assert ! ( upper == Some ( lower) ) ;
731+ assert_eq ! ( upper, Some ( lower) ) ;
748732 lower
749733 }
750734}
@@ -1330,18 +1314,12 @@ impl<'a, A, T: Iterator<A>> Iterator<A> for Filter<'a, A, T> {
13301314impl < ' a , A , T : DoubleEndedIterator < A > > DoubleEndedIterator < A > for Filter < ' a , A , T > {
13311315 #[ inline]
13321316 fn next_back ( & mut self ) -> Option < A > {
1333- loop {
1334- match self . iter . next_back ( ) {
1335- None => return None ,
1336- Some ( x) => {
1337- if ( self . predicate ) ( & x) {
1338- return Some ( x) ;
1339- } else {
1340- continue
1341- }
1342- }
1317+ for x in self . iter . by_ref ( ) . rev ( ) {
1318+ if ( self . predicate ) ( & x) {
1319+ return Some ( x) ;
13431320 }
13441321 }
1322+ None
13451323 }
13461324}
13471325
@@ -1375,17 +1353,13 @@ impl<'a, A, B, T: DoubleEndedIterator<A>> DoubleEndedIterator<B>
13751353for FilterMap < ' a , A , B , T > {
13761354 #[ inline]
13771355 fn next_back ( & mut self ) -> Option < B > {
1378- loop {
1379- match self . iter . next_back ( ) {
1380- None => return None ,
1381- Some ( x) => {
1382- match ( self . f ) ( x) {
1383- Some ( y) => return Some ( y) ,
1384- None => ( )
1385- }
1386- }
1356+ for x in self . iter . by_ref ( ) . rev ( ) {
1357+ match ( self . f ) ( x) {
1358+ Some ( y) => return Some ( y) ,
1359+ None => ( )
13871360 }
13881361 }
1362+ None
13891363 }
13901364}
13911365
@@ -1507,25 +1481,13 @@ pub struct SkipWhile<'a, A, T> {
15071481impl <' a, A , T : Iterator < A > > Iterator < A > for SkipWhile < ' a , A , T > {
15081482 #[ inline]
15091483 fn next ( & mut self ) -> Option < A > {
1510- let mut next = self . iter . next ( ) ;
1511- if self . flag {
1512- next
1513- } else {
1514- loop {
1515- match next {
1516- Some ( x) => {
1517- if ( self . predicate ) ( & x) {
1518- next = self . iter . next ( ) ;
1519- continue
1520- } else {
1521- self . flag = true ;
1522- return Some ( x)
1523- }
1524- }
1525- None => return None
1526- }
1484+ for x in self . iter {
1485+ if self . flag || !( self . predicate ) ( & x) {
1486+ self . flag = true ;
1487+ return Some ( x) ;
15271488 }
15281489 }
1490+ None
15291491 }
15301492
15311493 #[ inline]
0 commit comments