File tree 2 files changed +16
-6
lines changed
library/core/src/iter/traits
2 files changed +16
-6
lines changed Original file line number Diff line number Diff line change @@ -174,9 +174,14 @@ pub trait DoubleEndedIterator: Iterator {
174
174
/// ```
175
175
#[ inline]
176
176
#[ stable( feature = "iter_nth_back" , since = "1.37.0" ) ]
177
- fn nth_back ( & mut self , n : usize ) -> Option < Self :: Item > {
178
- self . advance_back_by ( n) . ok ( ) ?;
179
- self . next_back ( )
177
+ fn nth_back ( & mut self , mut n : usize ) -> Option < Self :: Item > {
178
+ for x in self . rev ( ) {
179
+ if n == 0 {
180
+ return Some ( x) ;
181
+ }
182
+ n -= 1 ;
183
+ }
184
+ None
180
185
}
181
186
182
187
/// This is the reverse version of [`Iterator::try_fold()`]: it takes
Original file line number Diff line number Diff line change @@ -363,9 +363,14 @@ pub trait Iterator {
363
363
/// ```
364
364
#[ inline]
365
365
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
366
- fn nth ( & mut self , n : usize ) -> Option < Self :: Item > {
367
- self . advance_by ( n) . ok ( ) ?;
368
- self . next ( )
366
+ fn nth ( & mut self , mut n : usize ) -> Option < Self :: Item > {
367
+ while let Some ( x) = self . next ( ) {
368
+ if n == 0 {
369
+ return Some ( x) ;
370
+ }
371
+ n -= 1 ;
372
+ }
373
+ None
369
374
}
370
375
371
376
/// Creates an iterator starting at the same point, but stepping by
You can’t perform that action at this time.
0 commit comments