2 use crate::hash::{Hash, Hasher};
4 /// An unbounded range (`..`).
6 /// `RangeFull` is primarily used as a [slicing index], its shorthand is `..`.
7 /// It cannot serve as an [`Iterator`] because it doesn't have a starting point.
11 /// The `..` syntax is a `RangeFull`:
14 /// assert_eq!((..), std::ops::RangeFull);
17 /// It does not have an [`IntoIterator`] implementation, so you can't use it in
18 /// a `for` loop directly. This won't compile:
20 /// ```compile_fail,E0277
26 /// Used as a [slicing index], `RangeFull` produces the full array as a slice.
29 /// let arr = [0, 1, 2, 3, 4];
30 /// assert_eq!(arr[ .. ], [0,1,2,3,4]); // RangeFull
31 /// assert_eq!(arr[ .. 3], [0,1,2 ]);
32 /// assert_eq!(arr[ ..=3], [0,1,2,3 ]);
33 /// assert_eq!(arr[1.. ], [ 1,2,3,4]);
34 /// assert_eq!(arr[1.. 3], [ 1,2 ]);
35 /// assert_eq!(arr[1..=3], [ 1,2,3 ]);
38 /// [`IntoIterator`]: ../iter/trait.Iterator.html
39 /// [`Iterator`]: ../iter/trait.IntoIterator.html
40 /// [slicing index]: ../slice/trait.SliceIndex.html
42 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
43 #[stable(feature = "rust1", since = "1.0.0")]
46 #[stable(feature = "rust1", since = "1.0.0")]
47 impl fmt::Debug for RangeFull {
48 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
53 /// A (half-open) range bounded inclusively below and exclusively above
56 /// The `Range` `start..end` contains all values with `x >= start` and
57 /// `x < end`. It is empty unless `start < end`.
62 /// assert_eq!((3..5), std::ops::Range { start: 3, end: 5 });
63 /// assert_eq!(3 + 4 + 5, (3..6).sum());
65 /// let arr = [0, 1, 2, 3, 4];
66 /// assert_eq!(arr[ .. ], [0,1,2,3,4]);
67 /// assert_eq!(arr[ .. 3], [0,1,2 ]);
68 /// assert_eq!(arr[ ..=3], [0,1,2,3 ]);
69 /// assert_eq!(arr[1.. ], [ 1,2,3,4]);
70 /// assert_eq!(arr[1.. 3], [ 1,2 ]); // Range
71 /// assert_eq!(arr[1..=3], [ 1,2,3 ]);
74 #[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186
75 #[stable(feature = "rust1", since = "1.0.0")]
76 pub struct Range<Idx> {
77 /// The lower bound of the range (inclusive).
78 #[stable(feature = "rust1", since = "1.0.0")]
80 /// The upper bound of the range (exclusive).
81 #[stable(feature = "rust1", since = "1.0.0")]
85 #[stable(feature = "rust1", since = "1.0.0")]
86 impl<Idx: fmt::Debug> fmt::Debug for Range<Idx> {
87 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
95 impl<Idx: PartialOrd<Idx>> Range<Idx> {
96 /// Returns `true` if `item` is contained in the range.
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 /// assert!( (0.0..1.0).contains(&0.5));
112 /// assert!(!(0.0..1.0).contains(&f32::NAN));
113 /// assert!(!(0.0..f32::NAN).contains(&0.5));
114 /// assert!(!(f32::NAN..1.0).contains(&0.5));
116 #[stable(feature = "range_contains", since = "1.35.0")]
117 pub fn contains<U>(&self, item: &U) -> bool
120 U: ?Sized + PartialOrd<Idx>,
122 <Self as RangeBounds<Idx>>::contains(self, item)
125 /// Returns `true` if the range contains no items.
130 /// #![feature(range_is_empty)]
132 /// assert!(!(3..5).is_empty());
133 /// assert!( (3..3).is_empty());
134 /// assert!( (3..2).is_empty());
137 /// The range is empty if either side is incomparable:
140 /// #![feature(range_is_empty)]
142 /// use std::f32::NAN;
143 /// assert!(!(3.0..5.0).is_empty());
144 /// assert!( (3.0..NAN).is_empty());
145 /// assert!( (NAN..5.0).is_empty());
147 #[unstable(feature = "range_is_empty", reason = "recently added", issue = "48111")]
148 pub fn is_empty(&self) -> bool {
149 !(self.start < self.end)
153 /// A range only bounded inclusively below (`start..`).
155 /// The `RangeFrom` `start..` contains all values with `x >= start`.
157 /// *Note*: Currently, no overflow checking is done for the [`Iterator`]
158 /// implementation; if you use an integer range and the integer overflows, it
159 /// might panic in debug mode or create an endless loop in release mode. **This
160 /// overflow behavior might change in the future.**
165 /// assert_eq!((2..), std::ops::RangeFrom { start: 2 });
166 /// assert_eq!(2 + 3 + 4, (2..).take(3).sum());
168 /// let arr = [0, 1, 2, 3, 4];
169 /// assert_eq!(arr[ .. ], [0,1,2,3,4]);
170 /// assert_eq!(arr[ .. 3], [0,1,2 ]);
171 /// assert_eq!(arr[ ..=3], [0,1,2,3 ]);
172 /// assert_eq!(arr[1.. ], [ 1,2,3,4]); // RangeFrom
173 /// assert_eq!(arr[1.. 3], [ 1,2 ]);
174 /// assert_eq!(arr[1..=3], [ 1,2,3 ]);
177 /// [`Iterator`]: ../iter/trait.IntoIterator.html
179 #[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186
180 #[stable(feature = "rust1", since = "1.0.0")]
181 pub struct RangeFrom<Idx> {
182 /// The lower bound of the range (inclusive).
183 #[stable(feature = "rust1", since = "1.0.0")]
187 #[stable(feature = "rust1", since = "1.0.0")]
188 impl<Idx: fmt::Debug> fmt::Debug for RangeFrom<Idx> {
189 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
190 self.start.fmt(fmt)?;
196 impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
197 /// Returns `true` if `item` is contained in the range.
204 /// assert!(!(3..).contains(&2));
205 /// assert!( (3..).contains(&3));
206 /// assert!( (3..).contains(&1_000_000_000));
208 /// assert!( (0.0..).contains(&0.5));
209 /// assert!(!(0.0..).contains(&f32::NAN));
210 /// assert!(!(f32::NAN..).contains(&0.5));
212 #[stable(feature = "range_contains", since = "1.35.0")]
213 pub fn contains<U>(&self, item: &U) -> bool
216 U: ?Sized + PartialOrd<Idx>,
218 <Self as RangeBounds<Idx>>::contains(self, item)
222 /// A range only bounded exclusively above (`..end`).
224 /// The `RangeTo` `..end` contains all values with `x < end`.
225 /// It cannot serve as an [`Iterator`] because it doesn't have a starting point.
229 /// The `..end` syntax is a `RangeTo`:
232 /// assert_eq!((..5), std::ops::RangeTo { end: 5 });
235 /// It does not have an [`IntoIterator`] implementation, so you can't use it in
236 /// a `for` loop directly. This won't compile:
238 /// ```compile_fail,E0277
239 /// // error[E0277]: the trait bound `std::ops::RangeTo<{integer}>:
240 /// // std::iter::Iterator` is not satisfied
246 /// When used as a [slicing index], `RangeTo` produces a slice of all array
247 /// elements before the index indicated by `end`.
250 /// let arr = [0, 1, 2, 3, 4];
251 /// assert_eq!(arr[ .. ], [0,1,2,3,4]);
252 /// assert_eq!(arr[ .. 3], [0,1,2 ]); // RangeTo
253 /// assert_eq!(arr[ ..=3], [0,1,2,3 ]);
254 /// assert_eq!(arr[1.. ], [ 1,2,3,4]);
255 /// assert_eq!(arr[1.. 3], [ 1,2 ]);
256 /// assert_eq!(arr[1..=3], [ 1,2,3 ]);
259 /// [`IntoIterator`]: ../iter/trait.Iterator.html
260 /// [`Iterator`]: ../iter/trait.IntoIterator.html
261 /// [slicing index]: ../slice/trait.SliceIndex.html
263 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
264 #[stable(feature = "rust1", since = "1.0.0")]
265 pub struct RangeTo<Idx> {
266 /// The upper bound of the range (exclusive).
267 #[stable(feature = "rust1", since = "1.0.0")]
271 #[stable(feature = "rust1", since = "1.0.0")]
272 impl<Idx: fmt::Debug> fmt::Debug for RangeTo<Idx> {
273 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
280 impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
281 /// Returns `true` if `item` is contained in the range.
288 /// assert!( (..5).contains(&-1_000_000_000));
289 /// assert!( (..5).contains(&4));
290 /// assert!(!(..5).contains(&5));
292 /// assert!( (..1.0).contains(&0.5));
293 /// assert!(!(..1.0).contains(&f32::NAN));
294 /// assert!(!(..f32::NAN).contains(&0.5));
296 #[stable(feature = "range_contains", since = "1.35.0")]
297 pub fn contains<U>(&self, item: &U) -> bool
300 U: ?Sized + PartialOrd<Idx>,
302 <Self as RangeBounds<Idx>>::contains(self, item)
306 /// A range bounded inclusively below and above (`start..=end`).
308 /// The `RangeInclusive` `start..=end` contains all values with `x >= start`
309 /// and `x <= end`. It is empty unless `start <= end`.
311 /// This iterator is [fused], but the specific values of `start` and `end` after
312 /// iteration has finished are **unspecified** other than that [`.is_empty()`]
313 /// will return `true` once no more values will be produced.
315 /// [fused]: ../iter/trait.FusedIterator.html
316 /// [`.is_empty()`]: #method.is_empty
321 /// assert_eq!((3..=5), std::ops::RangeInclusive::new(3, 5));
322 /// assert_eq!(3 + 4 + 5, (3..=5).sum());
324 /// let arr = [0, 1, 2, 3, 4];
325 /// assert_eq!(arr[ .. ], [0,1,2,3,4]);
326 /// assert_eq!(arr[ .. 3], [0,1,2 ]);
327 /// assert_eq!(arr[ ..=3], [0,1,2,3 ]);
328 /// assert_eq!(arr[1.. ], [ 1,2,3,4]);
329 /// assert_eq!(arr[1.. 3], [ 1,2 ]);
330 /// assert_eq!(arr[1..=3], [ 1,2,3 ]); // RangeInclusive
332 #[doc(alias = "..=")]
333 #[derive(Clone)] // not Copy -- see #27186
334 #[stable(feature = "inclusive_range", since = "1.26.0")]
335 pub struct RangeInclusive<Idx> {
336 // Note that the fields here are not public to allow changing the
337 // representation in the future; in particular, while we could plausibly
338 // expose start/end, modifying them without changing (future/current)
339 // private fields may lead to incorrect behavior, so we don't want to
340 // support that mode.
341 pub(crate) start: Idx,
343 pub(crate) is_empty: Option<bool>,
345 // - `None` when next() or next_back() was never called
346 // - `Some(false)` when `start < end`
347 // - `Some(true)` when `end < start`
348 // - `Some(false)` when `start == end` and the range hasn't yet completed iteration
349 // - `Some(true)` when `start == end` and the range has completed iteration
350 // The field cannot be a simple `bool` because the `..=` constructor can
351 // accept non-PartialOrd types, also we want the constructor to be const.
354 #[stable(feature = "inclusive_range", since = "1.26.0")]
355 impl<Idx: PartialEq> PartialEq for RangeInclusive<Idx> {
357 fn eq(&self, other: &Self) -> bool {
358 self.start == other.start
359 && self.end == other.end
360 && self.is_exhausted() == other.is_exhausted()
364 #[stable(feature = "inclusive_range", since = "1.26.0")]
365 impl<Idx: Eq> Eq for RangeInclusive<Idx> {}
367 #[stable(feature = "inclusive_range", since = "1.26.0")]
368 impl<Idx: Hash> Hash for RangeInclusive<Idx> {
369 fn hash<H: Hasher>(&self, state: &mut H) {
370 self.start.hash(state);
371 self.end.hash(state);
372 // Ideally we would hash `is_exhausted` here as well, but there's no
373 // way for us to call it.
377 impl<Idx> RangeInclusive<Idx> {
378 /// Creates a new inclusive range. Equivalent to writing `start..=end`.
383 /// use std::ops::RangeInclusive;
385 /// assert_eq!(3..=5, RangeInclusive::new(3, 5));
387 #[stable(feature = "inclusive_range_methods", since = "1.27.0")]
390 #[rustc_const_stable(feature = "const_range_new", since = "1.32.0")]
391 pub const fn new(start: Idx, end: Idx) -> Self {
392 Self { start, end, is_empty: None }
395 /// Returns the lower bound of the range (inclusive).
397 /// When using an inclusive range for iteration, the values of `start()` and
398 /// [`end()`] are unspecified after the iteration ended. To determine
399 /// whether the inclusive range is empty, use the [`is_empty()`] method
400 /// instead of comparing `start() > end()`.
402 /// Note: the value returned by this method is unspecified after the range
403 /// has been iterated to exhaustion.
405 /// [`end()`]: #method.end
406 /// [`is_empty()`]: #method.is_empty
411 /// assert_eq!((3..=5).start(), &3);
413 #[stable(feature = "inclusive_range_methods", since = "1.27.0")]
414 #[rustc_const_stable(feature = "const_inclusive_range_methods", since = "1.32.0")]
416 pub const fn start(&self) -> &Idx {
420 /// Returns the upper bound of the range (inclusive).
422 /// When using an inclusive range for iteration, the values of [`start()`]
423 /// and `end()` are unspecified after the iteration ended. To determine
424 /// whether the inclusive range is empty, use the [`is_empty()`] method
425 /// instead of comparing `start() > end()`.
427 /// Note: the value returned by this method is unspecified after the range
428 /// has been iterated to exhaustion.
430 /// [`start()`]: #method.start
431 /// [`is_empty()`]: #method.is_empty
436 /// assert_eq!((3..=5).end(), &5);
438 #[stable(feature = "inclusive_range_methods", since = "1.27.0")]
439 #[rustc_const_stable(feature = "const_inclusive_range_methods", since = "1.32.0")]
441 pub const fn end(&self) -> &Idx {
445 /// Destructures the `RangeInclusive` into (lower bound, upper (inclusive) bound).
447 /// Note: the value returned by this method is unspecified after the range
448 /// has been iterated to exhaustion.
453 /// assert_eq!((3..=5).into_inner(), (3, 5));
455 #[stable(feature = "inclusive_range_methods", since = "1.27.0")]
457 pub fn into_inner(self) -> (Idx, Idx) {
458 (self.start, self.end)
462 #[stable(feature = "inclusive_range", since = "1.26.0")]
463 impl<Idx: fmt::Debug> fmt::Debug for RangeInclusive<Idx> {
464 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
465 self.start.fmt(fmt)?;
472 impl<Idx: PartialEq<Idx>> RangeInclusive<Idx> {
473 // Returns true if this is a range that started non-empty, and was iterated
475 fn is_exhausted(&self) -> bool {
476 Some(true) == self.is_empty && self.start == self.end
480 impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
481 /// Returns `true` if `item` is contained in the range.
488 /// assert!(!(3..=5).contains(&2));
489 /// assert!( (3..=5).contains(&3));
490 /// assert!( (3..=5).contains(&4));
491 /// assert!( (3..=5).contains(&5));
492 /// assert!(!(3..=5).contains(&6));
494 /// assert!( (3..=3).contains(&3));
495 /// assert!(!(3..=2).contains(&3));
497 /// assert!( (0.0..=1.0).contains(&1.0));
498 /// assert!(!(0.0..=1.0).contains(&f32::NAN));
499 /// assert!(!(0.0..=f32::NAN).contains(&0.0));
500 /// assert!(!(f32::NAN..=1.0).contains(&1.0));
502 #[stable(feature = "range_contains", since = "1.35.0")]
503 pub fn contains<U>(&self, item: &U) -> bool
506 U: ?Sized + PartialOrd<Idx>,
508 <Self as RangeBounds<Idx>>::contains(self, item)
511 /// Returns `true` if the range contains no items.
516 /// #![feature(range_is_empty)]
518 /// assert!(!(3..=5).is_empty());
519 /// assert!(!(3..=3).is_empty());
520 /// assert!( (3..=2).is_empty());
523 /// The range is empty if either side is incomparable:
526 /// #![feature(range_is_empty)]
528 /// use std::f32::NAN;
529 /// assert!(!(3.0..=5.0).is_empty());
530 /// assert!( (3.0..=NAN).is_empty());
531 /// assert!( (NAN..=5.0).is_empty());
534 /// This method returns `true` after iteration has finished:
537 /// #![feature(range_is_empty)]
539 /// let mut r = 3..=5;
540 /// for _ in r.by_ref() {}
541 /// // Precise field values are unspecified here
542 /// assert!(r.is_empty());
544 #[unstable(feature = "range_is_empty", reason = "recently added", issue = "48111")]
546 pub fn is_empty(&self) -> bool {
547 self.is_empty.unwrap_or_else(|| !(self.start <= self.end))
550 // If this range's `is_empty` is field is unknown (`None`), update it to be a concrete value.
552 pub(crate) fn compute_is_empty(&mut self) {
553 if self.is_empty.is_none() {
554 self.is_empty = Some(!(self.start <= self.end));
559 /// A range only bounded inclusively above (`..=end`).
561 /// The `RangeToInclusive` `..=end` contains all values with `x <= end`.
562 /// It cannot serve as an [`Iterator`] because it doesn't have a starting point.
566 /// The `..=end` syntax is a `RangeToInclusive`:
569 /// assert_eq!((..=5), std::ops::RangeToInclusive{ end: 5 });
572 /// It does not have an [`IntoIterator`] implementation, so you can't use it in a
573 /// `for` loop directly. This won't compile:
575 /// ```compile_fail,E0277
576 /// // error[E0277]: the trait bound `std::ops::RangeToInclusive<{integer}>:
577 /// // std::iter::Iterator` is not satisfied
583 /// When used as a [slicing index], `RangeToInclusive` produces a slice of all
584 /// array elements up to and including the index indicated by `end`.
587 /// let arr = [0, 1, 2, 3, 4];
588 /// assert_eq!(arr[ .. ], [0,1,2,3,4]);
589 /// assert_eq!(arr[ .. 3], [0,1,2 ]);
590 /// assert_eq!(arr[ ..=3], [0,1,2,3 ]); // RangeToInclusive
591 /// assert_eq!(arr[1.. ], [ 1,2,3,4]);
592 /// assert_eq!(arr[1.. 3], [ 1,2 ]);
593 /// assert_eq!(arr[1..=3], [ 1,2,3 ]);
596 /// [`IntoIterator`]: ../iter/trait.Iterator.html
597 /// [`Iterator`]: ../iter/trait.IntoIterator.html
598 /// [slicing index]: ../slice/trait.SliceIndex.html
599 #[doc(alias = "..=")]
600 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
601 #[stable(feature = "inclusive_range", since = "1.26.0")]
602 pub struct RangeToInclusive<Idx> {
603 /// The upper bound of the range (inclusive)
604 #[stable(feature = "inclusive_range", since = "1.26.0")]
608 #[stable(feature = "inclusive_range", since = "1.26.0")]
609 impl<Idx: fmt::Debug> fmt::Debug for RangeToInclusive<Idx> {
610 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
617 impl<Idx: PartialOrd<Idx>> RangeToInclusive<Idx> {
618 /// Returns `true` if `item` is contained in the range.
625 /// assert!( (..=5).contains(&-1_000_000_000));
626 /// assert!( (..=5).contains(&5));
627 /// assert!(!(..=5).contains(&6));
629 /// assert!( (..=1.0).contains(&1.0));
630 /// assert!(!(..=1.0).contains(&f32::NAN));
631 /// assert!(!(..=f32::NAN).contains(&0.5));
633 #[stable(feature = "range_contains", since = "1.35.0")]
634 pub fn contains<U>(&self, item: &U) -> bool
637 U: ?Sized + PartialOrd<Idx>,
639 <Self as RangeBounds<Idx>>::contains(self, item)
643 // RangeToInclusive<Idx> cannot impl From<RangeTo<Idx>>
644 // because underflow would be possible with (..0).into()
646 /// An endpoint of a range of keys.
650 /// `Bound`s are range endpoints:
653 /// use std::ops::Bound::*;
654 /// use std::ops::RangeBounds;
656 /// assert_eq!((..100).start_bound(), Unbounded);
657 /// assert_eq!((1..12).start_bound(), Included(&1));
658 /// assert_eq!((1..12).end_bound(), Excluded(&12));
661 /// Using a tuple of `Bound`s as an argument to [`BTreeMap::range`].
662 /// Note that in most cases, it's better to use range syntax (`1..5`) instead.
665 /// use std::collections::BTreeMap;
666 /// use std::ops::Bound::{Excluded, Included, Unbounded};
668 /// let mut map = BTreeMap::new();
669 /// map.insert(3, "a");
670 /// map.insert(5, "b");
671 /// map.insert(8, "c");
673 /// for (key, value) in map.range((Excluded(3), Included(8))) {
674 /// println!("{}: {}", key, value);
677 /// assert_eq!(Some((&3, &"a")), map.range((Unbounded, Included(5))).next());
680 /// [`BTreeMap::range`]: ../../std/collections/btree_map/struct.BTreeMap.html#method.range
681 #[stable(feature = "collections_bound", since = "1.17.0")]
682 #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
684 /// An inclusive bound.
685 #[stable(feature = "collections_bound", since = "1.17.0")]
686 Included(#[stable(feature = "collections_bound", since = "1.17.0")] T),
687 /// An exclusive bound.
688 #[stable(feature = "collections_bound", since = "1.17.0")]
689 Excluded(#[stable(feature = "collections_bound", since = "1.17.0")] T),
690 /// An infinite endpoint. Indicates that there is no bound in this direction.
691 #[stable(feature = "collections_bound", since = "1.17.0")]
695 impl<T: Clone> Bound<&T> {
696 /// Map a `Bound<&T>` to a `Bound<T>` by cloning the contents of the bound.
701 /// #![feature(bound_cloned)]
702 /// use std::ops::Bound::*;
703 /// use std::ops::RangeBounds;
705 /// assert_eq!((1..12).start_bound(), Included(&1));
706 /// assert_eq!((1..12).start_bound().cloned(), Included(1));
708 #[unstable(feature = "bound_cloned", issue = "61356")]
709 pub fn cloned(self) -> Bound<T> {
711 Bound::Unbounded => Bound::Unbounded,
712 Bound::Included(x) => Bound::Included(x.clone()),
713 Bound::Excluded(x) => Bound::Excluded(x.clone()),
718 #[stable(feature = "collections_range", since = "1.28.0")]
719 /// `RangeBounds` is implemented by Rust's built-in range types, produced
720 /// by range syntax like `..`, `a..`, `..b`, `..=c`, `d..e`, or `f..=g`.
721 pub trait RangeBounds<T: ?Sized> {
722 /// Start index bound.
724 /// Returns the start value as a `Bound`.
730 /// use std::ops::Bound::*;
731 /// use std::ops::RangeBounds;
733 /// assert_eq!((..10).start_bound(), Unbounded);
734 /// assert_eq!((3..10).start_bound(), Included(&3));
737 #[stable(feature = "collections_range", since = "1.28.0")]
738 fn start_bound(&self) -> Bound<&T>;
742 /// Returns the end value as a `Bound`.
748 /// use std::ops::Bound::*;
749 /// use std::ops::RangeBounds;
751 /// assert_eq!((3..).end_bound(), Unbounded);
752 /// assert_eq!((3..10).end_bound(), Excluded(&10));
755 #[stable(feature = "collections_range", since = "1.28.0")]
756 fn end_bound(&self) -> Bound<&T>;
758 /// Returns `true` if `item` is contained in the range.
765 /// assert!( (3..5).contains(&4));
766 /// assert!(!(3..5).contains(&2));
768 /// assert!( (0.0..1.0).contains(&0.5));
769 /// assert!(!(0.0..1.0).contains(&f32::NAN));
770 /// assert!(!(0.0..f32::NAN).contains(&0.5));
771 /// assert!(!(f32::NAN..1.0).contains(&0.5));
772 #[stable(feature = "range_contains", since = "1.35.0")]
773 fn contains<U>(&self, item: &U) -> bool
776 U: ?Sized + PartialOrd<T>,
778 (match self.start_bound() {
779 Included(ref start) => *start <= item,
780 Excluded(ref start) => *start < item,
782 }) && (match self.end_bound() {
783 Included(ref end) => item <= *end,
784 Excluded(ref end) => item < *end,
790 use self::Bound::{Excluded, Included, Unbounded};
792 #[stable(feature = "collections_range", since = "1.28.0")]
793 impl<T: ?Sized> RangeBounds<T> for RangeFull {
794 fn start_bound(&self) -> Bound<&T> {
797 fn end_bound(&self) -> Bound<&T> {
802 #[stable(feature = "collections_range", since = "1.28.0")]
803 impl<T> RangeBounds<T> for RangeFrom<T> {
804 fn start_bound(&self) -> Bound<&T> {
805 Included(&self.start)
807 fn end_bound(&self) -> Bound<&T> {
812 #[stable(feature = "collections_range", since = "1.28.0")]
813 impl<T> RangeBounds<T> for RangeTo<T> {
814 fn start_bound(&self) -> Bound<&T> {
817 fn end_bound(&self) -> Bound<&T> {
822 #[stable(feature = "collections_range", since = "1.28.0")]
823 impl<T> RangeBounds<T> for Range<T> {
824 fn start_bound(&self) -> Bound<&T> {
825 Included(&self.start)
827 fn end_bound(&self) -> Bound<&T> {
832 #[stable(feature = "collections_range", since = "1.28.0")]
833 impl<T> RangeBounds<T> for RangeInclusive<T> {
834 fn start_bound(&self) -> Bound<&T> {
835 Included(&self.start)
837 fn end_bound(&self) -> Bound<&T> {
842 #[stable(feature = "collections_range", since = "1.28.0")]
843 impl<T> RangeBounds<T> for RangeToInclusive<T> {
844 fn start_bound(&self) -> Bound<&T> {
847 fn end_bound(&self) -> Bound<&T> {
852 #[stable(feature = "collections_range", since = "1.28.0")]
853 impl<T> RangeBounds<T> for (Bound<T>, Bound<T>) {
854 fn start_bound(&self) -> Bound<&T> {
856 (Included(ref start), _) => Included(start),
857 (Excluded(ref start), _) => Excluded(start),
858 (Unbounded, _) => Unbounded,
862 fn end_bound(&self) -> Bound<&T> {
864 (_, Included(ref end)) => Included(end),
865 (_, Excluded(ref end)) => Excluded(end),
866 (_, Unbounded) => Unbounded,
871 #[stable(feature = "collections_range", since = "1.28.0")]
872 impl<'a, T: ?Sized + 'a> RangeBounds<T> for (Bound<&'a T>, Bound<&'a T>) {
873 fn start_bound(&self) -> Bound<&T> {
877 fn end_bound(&self) -> Bound<&T> {
882 #[stable(feature = "collections_range", since = "1.28.0")]
883 impl<T> RangeBounds<T> for RangeFrom<&T> {
884 fn start_bound(&self) -> Bound<&T> {
887 fn end_bound(&self) -> Bound<&T> {
892 #[stable(feature = "collections_range", since = "1.28.0")]
893 impl<T> RangeBounds<T> for RangeTo<&T> {
894 fn start_bound(&self) -> Bound<&T> {
897 fn end_bound(&self) -> Bound<&T> {
902 #[stable(feature = "collections_range", since = "1.28.0")]
903 impl<T> RangeBounds<T> for Range<&T> {
904 fn start_bound(&self) -> Bound<&T> {
907 fn end_bound(&self) -> Bound<&T> {
912 #[stable(feature = "collections_range", since = "1.28.0")]
913 impl<T> RangeBounds<T> for RangeInclusive<&T> {
914 fn start_bound(&self) -> Bound<&T> {
917 fn end_bound(&self) -> Bound<&T> {
922 #[stable(feature = "collections_range", since = "1.28.0")]
923 impl<T> RangeBounds<T> for RangeToInclusive<&T> {
924 fn start_bound(&self) -> Bound<&T> {
927 fn end_bound(&self) -> Bound<&T> {