Check for exhaustion in RangeInclusive::contains
When a range has finished iteration, `is_empty` returns true, so it should also be the case that `contains` returns false.
This commit is contained in:
parent
cb2462c53f
commit
b62b352f47
1 changed files with 11 additions and 1 deletions
|
@ -479,13 +479,23 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
|
||||||
/// assert!(!(0.0..=f32::NAN).contains(&0.0));
|
/// assert!(!(0.0..=f32::NAN).contains(&0.0));
|
||||||
/// assert!(!(f32::NAN..=1.0).contains(&1.0));
|
/// assert!(!(f32::NAN..=1.0).contains(&1.0));
|
||||||
/// ```
|
/// ```
|
||||||
|
///
|
||||||
|
/// This method always returns `false` after iteration has finished:
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// let mut r = 3..=5;
|
||||||
|
/// assert!(r.contains(&3) && r.contains(&5));
|
||||||
|
/// for _ in r.by_ref() {}
|
||||||
|
/// // Precise field values are unspecified here
|
||||||
|
/// assert!(!r.contains(&3) && !r.contains(&5));
|
||||||
|
/// ```
|
||||||
#[stable(feature = "range_contains", since = "1.35.0")]
|
#[stable(feature = "range_contains", since = "1.35.0")]
|
||||||
pub fn contains<U>(&self, item: &U) -> bool
|
pub fn contains<U>(&self, item: &U) -> bool
|
||||||
where
|
where
|
||||||
Idx: PartialOrd<U>,
|
Idx: PartialOrd<U>,
|
||||||
U: ?Sized + PartialOrd<Idx>,
|
U: ?Sized + PartialOrd<Idx>,
|
||||||
{
|
{
|
||||||
<Self as RangeBounds<Idx>>::contains(self, item)
|
!self.exhausted && <Self as RangeBounds<Idx>>::contains(self, item)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `true` if the range contains no items.
|
/// Returns `true` if the range contains no items.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue