@@ -481,13 +481,11 @@ pub trait Iterator<A> {
481
481
/// ```
482
482
#[ inline]
483
483
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) }
489
486
n -= 1 ;
490
487
}
488
+ None
491
489
}
492
490
493
491
/// Loops through the entire iterator, returning the last element of the
@@ -518,11 +516,8 @@ pub trait Iterator<A> {
518
516
#[ inline]
519
517
fn fold < B > ( & mut self , init : B , f : |B , A | -> B ) -> B {
520
518
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) ;
526
521
}
527
522
accum
528
523
}
@@ -720,21 +715,10 @@ pub trait ExactSize<A> : DoubleEndedIterator<A> {
720
715
/// If no element matches, None is returned.
721
716
#[ inline]
722
717
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) ;
738
722
}
739
723
}
740
724
None
@@ -744,7 +728,7 @@ pub trait ExactSize<A> : DoubleEndedIterator<A> {
744
728
/// Return the exact length of the iterator.
745
729
fn len ( & self ) -> uint {
746
730
let ( lower, upper) = self . size_hint ( ) ;
747
- assert ! ( upper == Some ( lower) ) ;
731
+ assert_eq ! ( upper, Some ( lower) ) ;
748
732
lower
749
733
}
750
734
}
@@ -1330,18 +1314,12 @@ impl<'a, A, T: Iterator<A>> Iterator<A> for Filter<'a, A, T> {
1330
1314
impl < ' a , A , T : DoubleEndedIterator < A > > DoubleEndedIterator < A > for Filter < ' a , A , T > {
1331
1315
#[ inline]
1332
1316
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) ;
1343
1320
}
1344
1321
}
1322
+ None
1345
1323
}
1346
1324
}
1347
1325
@@ -1375,17 +1353,13 @@ impl<'a, A, B, T: DoubleEndedIterator<A>> DoubleEndedIterator<B>
1375
1353
for FilterMap < ' a , A , B , T > {
1376
1354
#[ inline]
1377
1355
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 => ( )
1387
1360
}
1388
1361
}
1362
+ None
1389
1363
}
1390
1364
}
1391
1365
@@ -1507,25 +1481,13 @@ pub struct SkipWhile<'a, A, T> {
1507
1481
impl <' a, A , T : Iterator < A > > Iterator < A > for SkipWhile < ' a , A , T > {
1508
1482
#[ inline]
1509
1483
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) ;
1527
1488
}
1528
1489
}
1490
+ None
1529
1491
}
1530
1492
1531
1493
#[ inline]
0 commit comments