@@ -608,7 +608,11 @@ pub trait Iterator {
608608 fn all < F > ( & mut self , mut f : F ) -> bool where
609609 Self : Sized , F : FnMut ( Self :: Item ) -> bool
610610 {
611- for x in self . by_ref ( ) { if !f ( x) { return false ; } }
611+ for x in self . by_ref ( ) {
612+ if !f ( x) {
613+ return false ;
614+ }
615+ }
612616 true
613617 }
614618
@@ -633,7 +637,11 @@ pub trait Iterator {
633637 Self : Sized ,
634638 F : FnMut ( Self :: Item ) -> bool
635639 {
636- for x in self . by_ref ( ) { if f ( x) { return true ; } }
640+ for x in self . by_ref ( ) {
641+ if f ( x) {
642+ return true ;
643+ }
644+ }
637645 false
638646 }
639647
@@ -1562,13 +1570,11 @@ impl<A, B> Iterator for Zip<A, B> where A: Iterator, B: Iterator
15621570
15631571 #[ inline]
15641572 fn next ( & mut self ) -> Option < ( A :: Item , B :: Item ) > {
1565- match self . a . next ( ) {
1566- None => None ,
1567- Some ( x) => match self . b . next ( ) {
1568- None => None ,
1569- Some ( y) => Some ( ( x, y) )
1570- }
1571- }
1573+ self . a . next ( ) . and_then ( |x| {
1574+ self . b . next ( ) . and_then ( |y| {
1575+ Some ( ( x, y) )
1576+ } )
1577+ } )
15721578 }
15731579
15741580 #[ inline]
@@ -1626,13 +1632,11 @@ impl<A, B> RandomAccessIterator for Zip<A, B> where
16261632
16271633 #[ inline]
16281634 fn idx ( & mut self , index : usize ) -> Option < ( A :: Item , B :: Item ) > {
1629- match self . a . idx ( index) {
1630- None => None ,
1631- Some ( x) => match self . b . idx ( index) {
1632- None => None ,
1633- Some ( y) => Some ( ( x, y) )
1634- }
1635- }
1635+ self . a . idx ( index) . and_then ( |x| {
1636+ self . b . idx ( index) . and_then ( |y| {
1637+ Some ( ( x, y) )
1638+ } )
1639+ } )
16361640 }
16371641}
16381642
@@ -1748,9 +1752,8 @@ impl<B, I: Iterator, F> Iterator for FilterMap<I, F>
17481752 #[ inline]
17491753 fn next ( & mut self ) -> Option < B > {
17501754 for x in self . iter . by_ref ( ) {
1751- match ( self . f ) ( x) {
1752- Some ( y) => return Some ( y) ,
1753- None => ( )
1755+ if let Some ( y) = ( self . f ) ( x) {
1756+ return Some ( y) ;
17541757 }
17551758 }
17561759 None
@@ -1770,9 +1773,8 @@ impl<B, I: DoubleEndedIterator, F> DoubleEndedIterator for FilterMap<I, F>
17701773 #[ inline]
17711774 fn next_back ( & mut self ) -> Option < B > {
17721775 for x in self . iter . by_ref ( ) . rev ( ) {
1773- match ( self . f ) ( x) {
1774- Some ( y) => return Some ( y) ,
1775- None => ( )
1776+ if let Some ( y) = ( self . f ) ( x) {
1777+ return Some ( y) ;
17761778 }
17771779 }
17781780 None
@@ -1794,14 +1796,11 @@ impl<I> Iterator for Enumerate<I> where I: Iterator {
17941796
17951797 #[ inline]
17961798 fn next ( & mut self ) -> Option < ( usize , <I as Iterator >:: Item ) > {
1797- match self . iter . next ( ) {
1798- Some ( a) => {
1799- let ret = Some ( ( self . count , a) ) ;
1800- self . count += 1 ;
1801- ret
1802- }
1803- _ => None
1804- }
1799+ self . iter . next ( ) . map ( |a| {
1800+ let ret = ( self . count , a) ;
1801+ self . count += 1 ;
1802+ ret
1803+ } )
18051804 }
18061805
18071806 #[ inline]
@@ -1816,13 +1815,10 @@ impl<I> DoubleEndedIterator for Enumerate<I> where
18161815{
18171816 #[ inline]
18181817 fn next_back ( & mut self ) -> Option < ( usize , <I as Iterator >:: Item ) > {
1819- match self . iter . next_back ( ) {
1820- Some ( a) => {
1821- let len = self . iter . len ( ) ;
1822- Some ( ( self . count + len, a) )
1823- }
1824- _ => None
1825- }
1818+ self . iter . next_back ( ) . map ( |a| {
1819+ let len = self . iter . len ( ) ;
1820+ ( self . count + len, a)
1821+ } )
18261822 }
18271823}
18281824
@@ -1835,10 +1831,7 @@ impl<I> RandomAccessIterator for Enumerate<I> where I: RandomAccessIterator {
18351831
18361832 #[ inline]
18371833 fn idx ( & mut self , index : usize ) -> Option < ( usize , <I as Iterator >:: Item ) > {
1838- match self . iter . idx ( index) {
1839- Some ( a) => Some ( ( self . count + index, a) ) ,
1840- _ => None ,
1841- }
1834+ self . iter . idx ( index) . map ( |a| ( self . count + index, a) )
18421835 }
18431836}
18441837
@@ -1865,19 +1858,18 @@ impl<I: Iterator> Iterator for Peekable<I> {
18651858
18661859 #[ inline]
18671860 fn next ( & mut self ) -> Option < I :: Item > {
1868- if self . peeked . is_some ( ) { self . peeked . take ( ) }
1869- else { self . iter . next ( ) }
1861+ match self . peeked {
1862+ Some ( _) => self . peeked . take ( ) ,
1863+ None => self . iter . next ( ) ,
1864+ }
18701865 }
18711866
18721867 #[ inline]
18731868 fn size_hint ( & self ) -> ( usize , Option < usize > ) {
18741869 let ( lo, hi) = self . iter . size_hint ( ) ;
18751870 if self . peeked . is_some ( ) {
18761871 let lo = lo. saturating_add ( 1 ) ;
1877- let hi = match hi {
1878- Some ( x) => x. checked_add ( 1 ) ,
1879- None => None
1880- } ;
1872+ let hi = hi. and_then ( |x| x. checked_add ( 1 ) ) ;
18811873 ( lo, hi)
18821874 } else {
18831875 ( lo, hi)
@@ -1966,17 +1958,14 @@ impl<I: Iterator, P> Iterator for TakeWhile<I, P>
19661958 if self . flag {
19671959 None
19681960 } else {
1969- match self . iter . next ( ) {
1970- Some ( x) => {
1971- if ( self . predicate ) ( & x) {
1972- Some ( x)
1973- } else {
1974- self . flag = true ;
1975- None
1976- }
1961+ self . iter . next ( ) . and_then ( |x| {
1962+ if ( self . predicate ) ( & x) {
1963+ Some ( x)
1964+ } else {
1965+ self . flag = true ;
1966+ None
19771967 }
1978- None => None
1979- }
1968+ } )
19801969 }
19811970 }
19821971
@@ -2030,11 +2019,7 @@ impl<I> Iterator for Skip<I> where I: Iterator {
20302019 let ( lower, upper) = self . iter . size_hint ( ) ;
20312020
20322021 let lower = lower. saturating_sub ( self . n ) ;
2033-
2034- let upper = match upper {
2035- Some ( x) => Some ( x. saturating_sub ( self . n ) ) ,
2036- None => None
2037- } ;
2022+ let upper = upper. map ( |x| x. saturating_sub ( self . n ) ) ;
20382023
20392024 ( lower, upper)
20402025 }
@@ -2316,9 +2301,8 @@ pub struct Inspect<I, F> {
23162301impl < I : Iterator , F > Inspect < I , F > where F : FnMut ( & I :: Item ) {
23172302 #[ inline]
23182303 fn do_inspect ( & mut self , elt : Option < I :: Item > ) -> Option < I :: Item > {
2319- match elt {
2320- Some ( ref a) => ( self . f ) ( a) ,
2321- None => ( )
2304+ if let Some ( ref a) = elt {
2305+ ( self . f ) ( a) ;
23222306 }
23232307
23242308 elt
@@ -2619,17 +2603,14 @@ impl<A: Step + One + Clone> Iterator for RangeInclusive<A> {
26192603
26202604 #[ inline]
26212605 fn next ( & mut self ) -> Option < A > {
2622- match self . range . next ( ) {
2623- Some ( x) => Some ( x) ,
2624- None => {
2625- if !self . done && self . range . start == self . range . end {
2626- self . done = true ;
2627- Some ( self . range . end . clone ( ) )
2628- } else {
2629- None
2630- }
2606+ self . range . next ( ) . or_else ( || {
2607+ if !self . done && self . range . start == self . range . end {
2608+ self . done = true ;
2609+ Some ( self . range . end . clone ( ) )
2610+ } else {
2611+ None
26312612 }
2632- }
2613+ } )
26332614 }
26342615
26352616 #[ inline]
@@ -2639,10 +2620,7 @@ impl<A: Step + One + Clone> Iterator for RangeInclusive<A> {
26392620 ( lo, hi)
26402621 } else {
26412622 let lo = lo. saturating_add ( 1 ) ;
2642- let hi = match hi {
2643- Some ( x) => x. checked_add ( 1 ) ,
2644- None => None
2645- } ;
2623+ let hi = hi. and_then ( |x| x. checked_add ( 1 ) ) ;
26462624 ( lo, hi)
26472625 }
26482626 }
@@ -2805,10 +2783,9 @@ impl<A: Step + One + Clone> Iterator for ops::Range<A> {
28052783
28062784 #[ inline]
28072785 fn size_hint ( & self ) -> ( usize , Option < usize > ) {
2808- if let Some ( hint) = Step :: steps_between ( & self . start , & self . end , & A :: one ( ) ) {
2809- ( hint, Some ( hint) )
2810- } else {
2811- ( 0 , None )
2786+ match Step :: steps_between ( & self . start , & self . end , & A :: one ( ) ) {
2787+ Some ( hint) => ( hint, Some ( hint) ) ,
2788+ None => ( 0 , None )
28122789 }
28132790 }
28142791}
@@ -2899,13 +2876,8 @@ pub fn iterate<T, F>(seed: T, f: F) -> Iterate<T, F> where
28992876 let & mut ( ref mut f, ref mut val, ref mut first) = st;
29002877 if * first {
29012878 * first = false ;
2902- } else {
2903- match val. take ( ) {
2904- Some ( x) => {
2905- * val = Some ( ( * f) ( x) )
2906- }
2907- None => { }
2908- }
2879+ } else if let Some ( x) = val. take ( ) {
2880+ * val = Some ( ( * f) ( x) )
29092881 }
29102882 val. clone ( )
29112883 }
0 commit comments