@@ -446,6 +446,20 @@ impl<Idx> RangeInclusive<Idx> {
446
446
}
447
447
}
448
448
449
+ impl RangeInclusive < usize > {
450
+ /// Converts to an exclusive `Range` for `SliceIndex` implementations.
451
+ /// The caller is responsible for dealing with `end == usize::MAX`.
452
+ #[ inline]
453
+ pub ( crate ) fn into_slice_range ( self ) -> Range < usize > {
454
+ // If we're not exhausted, we want to simply slice `start..end + 1`.
455
+ // If we are exhausted, then slicing with `end + 1..end + 1` gives us an
456
+ // empty range that is still subject to bounds-checks for that endpoint.
457
+ let exclusive_end = self . end + 1 ;
458
+ let start = if self . exhausted { exclusive_end } else { self . start } ;
459
+ start..exclusive_end
460
+ }
461
+ }
462
+
449
463
#[ stable( feature = "inclusive_range" , since = "1.26.0" ) ]
450
464
impl < Idx : fmt:: Debug > fmt:: Debug for RangeInclusive < Idx > {
451
465
fn fmt ( & self , fmt : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
@@ -479,6 +493,16 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
479
493
/// assert!(!(0.0..=f32::NAN).contains(&0.0));
480
494
/// assert!(!(f32::NAN..=1.0).contains(&1.0));
481
495
/// ```
496
+ ///
497
+ /// This method always returns `false` after iteration has finished:
498
+ ///
499
+ /// ```
500
+ /// let mut r = 3..=5;
501
+ /// assert!(r.contains(&3) && r.contains(&5));
502
+ /// for _ in r.by_ref() {}
503
+ /// // Precise field values are unspecified here
504
+ /// assert!(!r.contains(&3) && !r.contains(&5));
505
+ /// ```
482
506
#[ stable( feature = "range_contains" , since = "1.35.0" ) ]
483
507
pub fn contains < U > ( & self , item : & U ) -> bool
484
508
where
@@ -881,7 +905,13 @@ impl<T> RangeBounds<T> for RangeInclusive<T> {
881
905
Included ( & self . start )
882
906
}
883
907
fn end_bound ( & self ) -> Bound < & T > {
884
- Included ( & self . end )
908
+ if self . exhausted {
909
+ // When the iterator is exhausted, we usually have start == end,
910
+ // but we want the range to appear empty, containing nothing.
911
+ Excluded ( & self . end )
912
+ } else {
913
+ Included ( & self . end )
914
+ }
885
915
}
886
916
}
887
917
0 commit comments