File tree 2 files changed +30
-0
lines changed
2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -281,6 +281,19 @@ impl<A: Step> DoubleEndedIterator for ops::Range<A> {
281
281
None
282
282
}
283
283
}
284
+
285
+ #[ inline]
286
+ fn nth_back ( & mut self , n : usize ) -> Option < A > {
287
+ if let Some ( minus_n) = self . end . sub_usize ( n) {
288
+ if minus_n > self . start {
289
+ self . end = minus_n. sub_one ( ) ;
290
+ return Some ( self . end . clone ( ) )
291
+ }
292
+ }
293
+
294
+ self . end = self . start . clone ( ) ;
295
+ None
296
+ }
284
297
}
285
298
286
299
#[ stable( feature = "fused" , since = "1.26.0" ) ]
Original file line number Diff line number Diff line change @@ -1657,6 +1657,23 @@ fn test_range_nth() {
1657
1657
assert_eq ! ( r, 20 ..20 ) ;
1658
1658
}
1659
1659
1660
+ #[ test]
1661
+ fn test_range_nth_back ( ) {
1662
+ assert_eq ! ( ( 10 ..15 ) . nth_back( 0 ) , Some ( 14 ) ) ;
1663
+ assert_eq ! ( ( 10 ..15 ) . nth_back( 1 ) , Some ( 13 ) ) ;
1664
+ assert_eq ! ( ( 10 ..15 ) . nth_back( 4 ) , Some ( 10 ) ) ;
1665
+ assert_eq ! ( ( 10 ..15 ) . nth_back( 5 ) , None ) ;
1666
+ assert_eq ! ( ( -120 ..80_i8 ) . nth_back( 199 ) , Some ( -120 ) ) ;
1667
+
1668
+ let mut r = 10 ..20 ;
1669
+ assert_eq ! ( r. nth_back( 2 ) , Some ( 17 ) ) ;
1670
+ assert_eq ! ( r, 10 ..17 ) ;
1671
+ assert_eq ! ( r. nth_back( 2 ) , Some ( 14 ) ) ;
1672
+ assert_eq ! ( r, 10 ..14 ) ;
1673
+ assert_eq ! ( r. nth_back( 10 ) , None ) ;
1674
+ assert_eq ! ( r, 10 ..10 ) ;
1675
+ }
1676
+
1660
1677
#[ test]
1661
1678
fn test_range_from_nth ( ) {
1662
1679
assert_eq ! ( ( 10 ..) . nth( 0 ) , Some ( 10 ) ) ;
You can’t perform that action at this time.
0 commit comments