@@ -60,19 +60,19 @@ impl fmt::Debug for RangeFull {
60
60
/// (`start..end`).
61
61
///
62
62
/// The `Range` `start..end` contains all values with `x >= start` and
63
- /// `x < end`.
63
+ /// `x < end`. It is empty unless `start < end`.
64
64
///
65
65
/// # Examples
66
66
///
67
67
/// ```
68
68
/// assert_eq!((3..5), std::ops::Range { start: 3, end: 5 });
69
69
/// assert_eq!(3 + 4 + 5, (3..6).sum());
70
70
///
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
76
76
/// ```
77
77
#[ derive( Clone , PartialEq , Eq , Hash ) ] // not Copy -- see #27186
78
78
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -92,7 +92,6 @@ impl<Idx: fmt::Debug> fmt::Debug for Range<Idx> {
92
92
}
93
93
}
94
94
95
- #[ unstable( feature = "range_contains" , reason = "recently added as per RFC" , issue = "32311" ) ]
96
95
impl < Idx : PartialOrd < Idx > > Range < Idx > {
97
96
/// Returns `true` if `item` is contained in the range.
98
97
///
@@ -109,9 +108,37 @@ impl<Idx: PartialOrd<Idx>> Range<Idx> {
109
108
/// assert!(!(3..3).contains(3));
110
109
/// assert!(!(3..2).contains(3));
111
110
/// ```
111
+ #[ unstable( feature = "range_contains" , reason = "recently added as per RFC" , issue = "32311" ) ]
112
112
pub fn contains ( & self , item : Idx ) -> bool {
113
113
( self . start <= item) && ( item < self . end )
114
114
}
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
+ }
115
142
}
116
143
117
144
/// A range only bounded inclusively below (`start..`).
@@ -244,7 +271,14 @@ impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
244
271
/// An range bounded inclusively below and above (`start..=end`).
245
272
///
246
273
/// 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
248
282
///
249
283
/// # Examples
250
284
///
@@ -280,7 +314,6 @@ impl<Idx: fmt::Debug> fmt::Debug for RangeInclusive<Idx> {
280
314
}
281
315
}
282
316
283
- #[ unstable( feature = "range_contains" , reason = "recently added as per RFC" , issue = "32311" ) ]
284
317
impl < Idx : PartialOrd < Idx > > RangeInclusive < Idx > {
285
318
/// Returns `true` if `item` is contained in the range.
286
319
///
@@ -298,9 +331,48 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
298
331
/// assert!( (3..=3).contains(3));
299
332
/// assert!(!(3..=2).contains(3));
300
333
/// ```
334
+ #[ unstable( feature = "range_contains" , reason = "recently added as per RFC" , issue = "32311" ) ]
301
335
pub fn contains ( & self , item : Idx ) -> bool {
302
336
self . start <= item && item <= self . end
303
337
}
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
+ }
304
376
}
305
377
306
378
/// A range only bounded inclusively above (`..=end`).
0 commit comments