@@ -60,19 +60,19 @@ impl fmt::Debug for RangeFull {
6060/// (`start..end`).
6161///
6262/// The `Range` `start..end` contains all values with `x >= start` and
63- /// `x < end`.
63+ /// `x < end`. It is empty unless `start < end`.
6464///
6565/// # Examples
6666///
6767/// ```
6868/// assert_eq!((3..5), std::ops::Range { start: 3, end: 5 });
6969/// assert_eq!(3 + 4 + 5, (3..6).sum());
7070///
71- /// let arr = [0, 1, 2, 3 ];
72- /// assert_eq!(arr[ .. ], [0,1,2,3 ]);
73- /// assert_eq!(arr[ ..3], [0,1,2 ]);
74- /// assert_eq!(arr[1.. ], [ 1,2,3 ]);
75- /// assert_eq!(arr[1..3], [ 1,2 ]); // Range
71+ /// let arr = ['a', 'b', 'c', 'd' ];
72+ /// assert_eq!(arr[ .. ], ['a', 'b', 'c', 'd' ]);
73+ /// assert_eq!(arr[ ..3], ['a', 'b', 'c', ]);
74+ /// assert_eq!(arr[1.. ], [ 'b', 'c', 'd' ]);
75+ /// assert_eq!(arr[1..3], [ 'b', 'c' ]); // Range
7676/// ```
7777#[ derive( Clone , PartialEq , Eq , Hash ) ] // not Copy -- see #27186
7878#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -92,7 +92,6 @@ impl<Idx: fmt::Debug> fmt::Debug for Range<Idx> {
9292 }
9393}
9494
95- #[ unstable( feature = "range_contains" , reason = "recently added as per RFC" , issue = "32311" ) ]
9695impl < Idx : PartialOrd < Idx > > Range < Idx > {
9796 /// Returns `true` if `item` is contained in the range.
9897 ///
@@ -109,9 +108,37 @@ impl<Idx: PartialOrd<Idx>> Range<Idx> {
109108 /// assert!(!(3..3).contains(3));
110109 /// assert!(!(3..2).contains(3));
111110 /// ```
111+ #[ unstable( feature = "range_contains" , reason = "recently added as per RFC" , issue = "32311" ) ]
112112 pub fn contains ( & self , item : Idx ) -> bool {
113113 ( self . start <= item) && ( item < self . end )
114114 }
115+
116+ /// Returns `true` if the range contains no items.
117+ ///
118+ /// # Examples
119+ ///
120+ /// ```
121+ /// #![feature(range_is_empty)]
122+ ///
123+ /// assert!(!(3..5).is_empty());
124+ /// assert!( (3..3).is_empty());
125+ /// assert!( (3..2).is_empty());
126+ /// ```
127+ ///
128+ /// The range is empty if either side is incomparable:
129+ ///
130+ /// ```
131+ /// #![feature(range_is_empty,inclusive_range_syntax)]
132+ ///
133+ /// use std::f32::NAN;
134+ /// assert!(!(3.0..5.0).is_empty());
135+ /// assert!( (3.0..NAN).is_empty());
136+ /// assert!( (NAN..5.0).is_empty());
137+ /// ```
138+ #[ unstable( feature = "range_is_empty" , reason = "recently added" , issue = "48111" ) ]
139+ pub fn is_empty ( & self ) -> bool {
140+ !( self . start < self . end )
141+ }
115142}
116143
117144/// A range only bounded inclusively below (`start..`).
@@ -244,7 +271,14 @@ impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
244271/// An range bounded inclusively below and above (`start..=end`).
245272///
246273/// The `RangeInclusive` `start..=end` contains all values with `x >= start`
247- /// and `x <= end`.
274+ /// and `x <= end`. It is empty unless `start <= end`.
275+ ///
276+ /// This iterator is [fused], but the specific values of `start` and `end` after
277+ /// iteration has finished are **unspecified** other than that [`.is_empty()`]
278+ /// will return `true` once no more values will be produced.
279+ ///
280+ /// [fused]: ../iter/trait.FusedIterator.html
281+ /// [`.is_empty()`]: #method.is_empty
248282///
249283/// # Examples
250284///
@@ -280,7 +314,6 @@ impl<Idx: fmt::Debug> fmt::Debug for RangeInclusive<Idx> {
280314 }
281315}
282316
283- #[ unstable( feature = "range_contains" , reason = "recently added as per RFC" , issue = "32311" ) ]
284317impl < Idx : PartialOrd < Idx > > RangeInclusive < Idx > {
285318 /// Returns `true` if `item` is contained in the range.
286319 ///
@@ -298,9 +331,48 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
298331 /// assert!( (3..=3).contains(3));
299332 /// assert!(!(3..=2).contains(3));
300333 /// ```
334+ #[ unstable( feature = "range_contains" , reason = "recently added as per RFC" , issue = "32311" ) ]
301335 pub fn contains ( & self , item : Idx ) -> bool {
302336 self . start <= item && item <= self . end
303337 }
338+
339+ /// Returns `true` if the range contains no items.
340+ ///
341+ /// # Examples
342+ ///
343+ /// ```
344+ /// #![feature(range_is_empty,inclusive_range_syntax)]
345+ ///
346+ /// assert!(!(3..=5).is_empty());
347+ /// assert!(!(3..=3).is_empty());
348+ /// assert!( (3..=2).is_empty());
349+ /// ```
350+ ///
351+ /// The range is empty if either side is incomparable:
352+ ///
353+ /// ```
354+ /// #![feature(range_is_empty,inclusive_range_syntax)]
355+ ///
356+ /// use std::f32::NAN;
357+ /// assert!(!(3.0..=5.0).is_empty());
358+ /// assert!( (3.0..=NAN).is_empty());
359+ /// assert!( (NAN..=5.0).is_empty());
360+ /// ```
361+ ///
362+ /// This method returns `true` after iteration has finished:
363+ ///
364+ /// ```
365+ /// #![feature(range_is_empty,inclusive_range_syntax)]
366+ ///
367+ /// let mut r = 3..=5;
368+ /// for _ in r.by_ref() {}
369+ /// // Precise field values are unspecified here
370+ /// assert!(r.is_empty());
371+ /// ```
372+ #[ unstable( feature = "range_is_empty" , reason = "recently added" , issue = "48111" ) ]
373+ pub fn is_empty ( & self ) -> bool {
374+ !( self . start <= self . end )
375+ }
304376}
305377
306378/// A range only bounded inclusively above (`..=end`).
0 commit comments