1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 /// An unbounded range (`..`).
15 /// `RangeFull` is primarily used as a [slicing index], its shorthand is `..`.
16 /// It cannot serve as an [`Iterator`] because it doesn't have a starting point.
20 /// The `..` syntax is a `RangeFull`:
23 /// assert_eq!((..), std::ops::RangeFull);
26 /// It does not have an [`IntoIterator`] implementation, so you can't use it in
27 /// a `for` loop directly. This won't compile:
29 /// ```compile_fail,E0277
35 /// Used as a [slicing index], `RangeFull` produces the full array as a slice.
38 /// let arr = [0, 1, 2, 3];
39 /// assert_eq!(arr[ .. ], [0,1,2,3]); // RangeFull
40 /// assert_eq!(arr[ ..3], [0,1,2 ]);
41 /// assert_eq!(arr[1.. ], [ 1,2,3]);
42 /// assert_eq!(arr[1..3], [ 1,2 ]);
45 /// [`IntoIterator`]: ../iter/trait.Iterator.html
46 /// [`Iterator`]: ../iter/trait.IntoIterator.html
47 /// [slicing index]: ../slice/trait.SliceIndex.html
48 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
49 #[stable(feature = "rust1", since = "1.0.0")]
52 #[stable(feature = "rust1", since = "1.0.0")]
53 impl fmt::Debug for RangeFull {
54 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
59 /// A (half-open) range bounded inclusively below and exclusively above
62 /// The `Range` `start..end` contains all values with `x >= start` and
63 /// `x < end`. It is empty unless `start < end`.
68 /// assert_eq!((3..5), std::ops::Range { start: 3, end: 5 });
69 /// assert_eq!(3 + 4 + 5, (3..6).sum());
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
77 #[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186
78 #[stable(feature = "rust1", since = "1.0.0")]
79 pub struct Range<Idx> {
80 /// The lower bound of the range (inclusive).
81 #[stable(feature = "rust1", since = "1.0.0")]
83 /// The upper bound of the range (exclusive).
84 #[stable(feature = "rust1", since = "1.0.0")]
88 #[stable(feature = "rust1", since = "1.0.0")]
89 impl<Idx: fmt::Debug> fmt::Debug for Range<Idx> {
90 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
91 write!(fmt, "{:?}..{:?}", self.start, self.end)
95 impl<Idx: PartialOrd<Idx>> Range<Idx> {
96 /// Returns `true` if `item` is contained in the range.
101 /// #![feature(range_contains)]
103 /// assert!(!(3..5).contains(2));
104 /// assert!( (3..5).contains(3));
105 /// assert!( (3..5).contains(4));
106 /// assert!(!(3..5).contains(5));
108 /// assert!(!(3..3).contains(3));
109 /// assert!(!(3..2).contains(3));
111 #[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
112 pub fn contains(&self, item: Idx) -> bool {
113 (self.start <= item) && (item < self.end)
116 /// Returns `true` if the range contains no items.
121 /// #![feature(range_is_empty)]
123 /// assert!(!(3..5).is_empty());
124 /// assert!( (3..3).is_empty());
125 /// assert!( (3..2).is_empty());
128 /// The range is empty if either side is incomparable:
131 /// #![feature(range_is_empty,inclusive_range_syntax)]
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());
138 #[unstable(feature = "range_is_empty", reason = "recently added", issue = "48111")]
139 pub fn is_empty(&self) -> bool {
140 !(self.start < self.end)
144 /// A range only bounded inclusively below (`start..`).
146 /// The `RangeFrom` `start..` contains all values with `x >= start`.
148 /// *Note*: Currently, no overflow checking is done for the [`Iterator`]
149 /// implementation; if you use an integer range and the integer overflows, it
150 /// might panic in debug mode or create an endless loop in release mode. **This
151 /// overflow behavior might change in the future.**
156 /// assert_eq!((2..), std::ops::RangeFrom { start: 2 });
157 /// assert_eq!(2 + 3 + 4, (2..).take(3).sum());
159 /// let arr = [0, 1, 2, 3];
160 /// assert_eq!(arr[ .. ], [0,1,2,3]);
161 /// assert_eq!(arr[ ..3], [0,1,2 ]);
162 /// assert_eq!(arr[1.. ], [ 1,2,3]); // RangeFrom
163 /// assert_eq!(arr[1..3], [ 1,2 ]);
166 /// [`Iterator`]: ../iter/trait.IntoIterator.html
167 #[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186
168 #[stable(feature = "rust1", since = "1.0.0")]
169 pub struct RangeFrom<Idx> {
170 /// The lower bound of the range (inclusive).
171 #[stable(feature = "rust1", since = "1.0.0")]
175 #[stable(feature = "rust1", since = "1.0.0")]
176 impl<Idx: fmt::Debug> fmt::Debug for RangeFrom<Idx> {
177 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
178 write!(fmt, "{:?}..", self.start)
182 #[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
183 impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
184 /// Returns `true` if `item` is contained in the range.
189 /// #![feature(range_contains)]
191 /// assert!(!(3..).contains(2));
192 /// assert!( (3..).contains(3));
193 /// assert!( (3..).contains(1_000_000_000));
195 pub fn contains(&self, item: Idx) -> bool {
200 /// A range only bounded exclusively above (`..end`).
202 /// The `RangeTo` `..end` contains all values with `x < end`.
203 /// It cannot serve as an [`Iterator`] because it doesn't have a starting point.
207 /// The `..end` syntax is a `RangeTo`:
210 /// assert_eq!((..5), std::ops::RangeTo { end: 5 });
213 /// It does not have an [`IntoIterator`] implementation, so you can't use it in
214 /// a `for` loop directly. This won't compile:
216 /// ```compile_fail,E0277
217 /// // error[E0277]: the trait bound `std::ops::RangeTo<{integer}>:
218 /// // std::iter::Iterator` is not satisfied
224 /// When used as a [slicing index], `RangeTo` produces a slice of all array
225 /// elements before the index indicated by `end`.
228 /// let arr = [0, 1, 2, 3];
229 /// assert_eq!(arr[ .. ], [0,1,2,3]);
230 /// assert_eq!(arr[ ..3], [0,1,2 ]); // RangeTo
231 /// assert_eq!(arr[1.. ], [ 1,2,3]);
232 /// assert_eq!(arr[1..3], [ 1,2 ]);
235 /// [`IntoIterator`]: ../iter/trait.Iterator.html
236 /// [`Iterator`]: ../iter/trait.IntoIterator.html
237 /// [slicing index]: ../slice/trait.SliceIndex.html
238 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
239 #[stable(feature = "rust1", since = "1.0.0")]
240 pub struct RangeTo<Idx> {
241 /// The upper bound of the range (exclusive).
242 #[stable(feature = "rust1", since = "1.0.0")]
246 #[stable(feature = "rust1", since = "1.0.0")]
247 impl<Idx: fmt::Debug> fmt::Debug for RangeTo<Idx> {
248 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
249 write!(fmt, "..{:?}", self.end)
253 #[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
254 impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
255 /// Returns `true` if `item` is contained in the range.
260 /// #![feature(range_contains)]
262 /// assert!( (..5).contains(-1_000_000_000));
263 /// assert!( (..5).contains(4));
264 /// assert!(!(..5).contains(5));
266 pub fn contains(&self, item: Idx) -> bool {
271 /// An range bounded inclusively below and above (`start..=end`).
273 /// The `RangeInclusive` `start..=end` contains all values with `x >= start`
274 /// and `x <= end`. It is empty unless `start <= end`.
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.
280 /// [fused]: ../iter/trait.FusedIterator.html
281 /// [`.is_empty()`]: #method.is_empty
286 /// #![feature(inclusive_range,inclusive_range_syntax)]
288 /// assert_eq!((3..=5), std::ops::RangeInclusive { start: 3, end: 5 });
289 /// assert_eq!(3 + 4 + 5, (3..=5).sum());
291 /// let arr = [0, 1, 2, 3];
292 /// assert_eq!(arr[ ..=2], [0,1,2 ]);
293 /// assert_eq!(arr[1..=2], [ 1,2 ]); // RangeInclusive
295 #[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186
296 #[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
297 pub struct RangeInclusive<Idx> {
298 /// The lower bound of the range (inclusive).
299 #[unstable(feature = "inclusive_range",
300 reason = "recently added, follows RFC",
303 /// The upper bound of the range (inclusive).
304 #[unstable(feature = "inclusive_range",
305 reason = "recently added, follows RFC",
310 #[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
311 impl<Idx: fmt::Debug> fmt::Debug for RangeInclusive<Idx> {
312 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
313 write!(fmt, "{:?}..={:?}", self.start, self.end)
317 impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
318 /// Returns `true` if `item` is contained in the range.
323 /// #![feature(range_contains,inclusive_range_syntax)]
325 /// assert!(!(3..=5).contains(2));
326 /// assert!( (3..=5).contains(3));
327 /// assert!( (3..=5).contains(4));
328 /// assert!( (3..=5).contains(5));
329 /// assert!(!(3..=5).contains(6));
331 /// assert!( (3..=3).contains(3));
332 /// assert!(!(3..=2).contains(3));
334 #[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
335 pub fn contains(&self, item: Idx) -> bool {
336 self.start <= item && item <= self.end
339 /// Returns `true` if the range contains no items.
344 /// #![feature(range_is_empty,inclusive_range_syntax)]
346 /// assert!(!(3..=5).is_empty());
347 /// assert!(!(3..=3).is_empty());
348 /// assert!( (3..=2).is_empty());
351 /// The range is empty if either side is incomparable:
354 /// #![feature(range_is_empty,inclusive_range_syntax)]
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());
362 /// This method returns `true` after iteration has finished:
365 /// #![feature(range_is_empty,inclusive_range_syntax)]
367 /// let mut r = 3..=5;
368 /// for _ in r.by_ref() {}
369 /// // Precise field values are unspecified here
370 /// assert!(r.is_empty());
372 #[unstable(feature = "range_is_empty", reason = "recently added", issue = "48111")]
373 pub fn is_empty(&self) -> bool {
374 !(self.start <= self.end)
378 /// A range only bounded inclusively above (`..=end`).
380 /// The `RangeToInclusive` `..=end` contains all values with `x <= end`.
381 /// It cannot serve as an [`Iterator`] because it doesn't have a starting point.
385 /// The `..=end` syntax is a `RangeToInclusive`:
388 /// #![feature(inclusive_range,inclusive_range_syntax)]
389 /// assert_eq!((..=5), std::ops::RangeToInclusive{ end: 5 });
392 /// It does not have an [`IntoIterator`] implementation, so you can't use it in a
393 /// `for` loop directly. This won't compile:
395 /// ```compile_fail,E0277
396 /// #![feature(inclusive_range_syntax)]
398 /// // error[E0277]: the trait bound `std::ops::RangeToInclusive<{integer}>:
399 /// // std::iter::Iterator` is not satisfied
405 /// When used as a [slicing index], `RangeToInclusive` produces a slice of all
406 /// array elements up to and including the index indicated by `end`.
409 /// #![feature(inclusive_range_syntax)]
411 /// let arr = [0, 1, 2, 3];
412 /// assert_eq!(arr[ ..=2], [0,1,2 ]); // RangeToInclusive
413 /// assert_eq!(arr[1..=2], [ 1,2 ]);
416 /// [`IntoIterator`]: ../iter/trait.Iterator.html
417 /// [`Iterator`]: ../iter/trait.IntoIterator.html
418 /// [slicing index]: ../slice/trait.SliceIndex.html
419 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
420 #[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
421 pub struct RangeToInclusive<Idx> {
422 /// The upper bound of the range (inclusive)
423 #[unstable(feature = "inclusive_range",
424 reason = "recently added, follows RFC",
429 #[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
430 impl<Idx: fmt::Debug> fmt::Debug for RangeToInclusive<Idx> {
431 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
432 write!(fmt, "..={:?}", self.end)
436 #[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
437 impl<Idx: PartialOrd<Idx>> RangeToInclusive<Idx> {
438 /// Returns `true` if `item` is contained in the range.
443 /// #![feature(range_contains,inclusive_range_syntax)]
445 /// assert!( (..=5).contains(-1_000_000_000));
446 /// assert!( (..=5).contains(5));
447 /// assert!(!(..=5).contains(6));
449 pub fn contains(&self, item: Idx) -> bool {
454 // RangeToInclusive<Idx> cannot impl From<RangeTo<Idx>>
455 // because underflow would be possible with (..0).into()