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 pub(crate) start: Idx,
338 pub(crate) is_empty: Option<bool>,
340 // - `None` when next() or next_back() was never called
341 // - `Some(false)` when `start <= end` assuming no overflow
342 // - `Some(true)` otherwise
343 // The field cannot be a simple `bool` because the `..=` constructor can
344 // accept non-PartialOrd types, also we want the constructor to be const.
347 trait RangeInclusiveEquality: Sized {
348 fn canonicalized_is_empty(range: &RangeInclusive<Self>) -> bool;
351 impl<T> RangeInclusiveEquality for T {
353 default fn canonicalized_is_empty(range: &RangeInclusive<Self>) -> bool {
354 range.is_empty.unwrap_or_default()
358 impl<T: PartialOrd> RangeInclusiveEquality for T {
360 fn canonicalized_is_empty(range: &RangeInclusive<Self>) -> bool {
365 #[stable(feature = "inclusive_range", since = "1.26.0")]
366 impl<Idx: PartialEq> PartialEq for RangeInclusive<Idx> {
368 fn eq(&self, other: &Self) -> bool {
369 self.start == other.start
370 && self.end == other.end
371 && RangeInclusiveEquality::canonicalized_is_empty(self)
372 == RangeInclusiveEquality::canonicalized_is_empty(other)
376 #[stable(feature = "inclusive_range", since = "1.26.0")]
377 impl<Idx: Eq> Eq for RangeInclusive<Idx> {}
379 #[stable(feature = "inclusive_range", since = "1.26.0")]
380 impl<Idx: Hash> Hash for RangeInclusive<Idx> {
381 fn hash<H: Hasher>(&self, state: &mut H) {
382 self.start.hash(state);
383 self.end.hash(state);
384 RangeInclusiveEquality::canonicalized_is_empty(self).hash(state);
388 impl<Idx> RangeInclusive<Idx> {
389 /// Creates a new inclusive range. Equivalent to writing `start..=end`.
394 /// use std::ops::RangeInclusive;
396 /// assert_eq!(3..=5, RangeInclusive::new(3, 5));
398 #[stable(feature = "inclusive_range_methods", since = "1.27.0")]
401 pub const fn new(start: Idx, end: Idx) -> Self {
409 /// Returns the lower bound of the range (inclusive).
411 /// When using an inclusive range for iteration, the values of `start()` and
412 /// [`end()`] are unspecified after the iteration ended. To determine
413 /// whether the inclusive range is empty, use the [`is_empty()`] method
414 /// instead of comparing `start() > end()`.
416 /// Note: the value returned by this method is unspecified after the range
417 /// has been iterated to exhaustion.
419 /// [`end()`]: #method.end
420 /// [`is_empty()`]: #method.is_empty
425 /// assert_eq!((3..=5).start(), &3);
427 #[stable(feature = "inclusive_range_methods", since = "1.27.0")]
429 pub const fn start(&self) -> &Idx {
433 /// Returns the upper bound of the range (inclusive).
435 /// When using an inclusive range for iteration, the values of [`start()`]
436 /// and `end()` are unspecified after the iteration ended. To determine
437 /// whether the inclusive range is empty, use the [`is_empty()`] method
438 /// instead of comparing `start() > end()`.
440 /// Note: the value returned by this method is unspecified after the range
441 /// has been iterated to exhaustion.
443 /// [`start()`]: #method.start
444 /// [`is_empty()`]: #method.is_empty
449 /// assert_eq!((3..=5).end(), &5);
451 #[stable(feature = "inclusive_range_methods", since = "1.27.0")]
453 pub const fn end(&self) -> &Idx {
457 /// Destructures the `RangeInclusive` into (lower bound, upper (inclusive) bound).
459 /// Note: the value returned by this method is unspecified after the range
460 /// has been iterated to exhaustion.
465 /// assert_eq!((3..=5).into_inner(), (3, 5));
467 #[stable(feature = "inclusive_range_methods", since = "1.27.0")]
469 pub fn into_inner(self) -> (Idx, Idx) {
470 (self.start, self.end)
474 #[stable(feature = "inclusive_range", since = "1.26.0")]
475 impl<Idx: fmt::Debug> fmt::Debug for RangeInclusive<Idx> {
476 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
477 self.start.fmt(fmt)?;
484 impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
485 /// Returns `true` if `item` is contained in the range.
492 /// assert!(!(3..=5).contains(&2));
493 /// assert!( (3..=5).contains(&3));
494 /// assert!( (3..=5).contains(&4));
495 /// assert!( (3..=5).contains(&5));
496 /// assert!(!(3..=5).contains(&6));
498 /// assert!( (3..=3).contains(&3));
499 /// assert!(!(3..=2).contains(&3));
501 /// assert!( (0.0..=1.0).contains(&1.0));
502 /// assert!(!(0.0..=1.0).contains(&f32::NAN));
503 /// assert!(!(0.0..=f32::NAN).contains(&0.0));
504 /// assert!(!(f32::NAN..=1.0).contains(&1.0));
506 #[stable(feature = "range_contains", since = "1.35.0")]
507 pub fn contains<U>(&self, item: &U) -> bool
510 U: ?Sized + PartialOrd<Idx>,
512 <Self as RangeBounds<Idx>>::contains(self, item)
515 /// Returns `true` if the range contains no items.
520 /// #![feature(range_is_empty)]
522 /// assert!(!(3..=5).is_empty());
523 /// assert!(!(3..=3).is_empty());
524 /// assert!( (3..=2).is_empty());
527 /// The range is empty if either side is incomparable:
530 /// #![feature(range_is_empty)]
532 /// use std::f32::NAN;
533 /// assert!(!(3.0..=5.0).is_empty());
534 /// assert!( (3.0..=NAN).is_empty());
535 /// assert!( (NAN..=5.0).is_empty());
538 /// This method returns `true` after iteration has finished:
541 /// #![feature(range_is_empty)]
543 /// let mut r = 3..=5;
544 /// for _ in r.by_ref() {}
545 /// // Precise field values are unspecified here
546 /// assert!(r.is_empty());
548 #[unstable(feature = "range_is_empty", reason = "recently added", issue = "48111")]
550 pub fn is_empty(&self) -> bool {
551 self.is_empty.unwrap_or_else(|| !(self.start <= self.end))
554 // If this range's `is_empty` is field is unknown (`None`), update it to be a concrete value.
556 pub(crate) fn compute_is_empty(&mut self) {
557 if self.is_empty.is_none() {
558 self.is_empty = Some(!(self.start <= self.end));
563 /// A range only bounded inclusively above (`..=end`).
565 /// The `RangeToInclusive` `..=end` contains all values with `x <= end`.
566 /// It cannot serve as an [`Iterator`] because it doesn't have a starting point.
570 /// The `..=end` syntax is a `RangeToInclusive`:
573 /// assert_eq!((..=5), std::ops::RangeToInclusive{ end: 5 });
576 /// It does not have an [`IntoIterator`] implementation, so you can't use it in a
577 /// `for` loop directly. This won't compile:
579 /// ```compile_fail,E0277
580 /// // error[E0277]: the trait bound `std::ops::RangeToInclusive<{integer}>:
581 /// // std::iter::Iterator` is not satisfied
587 /// When used as a [slicing index], `RangeToInclusive` produces a slice of all
588 /// array elements up to and including the index indicated by `end`.
591 /// let arr = [0, 1, 2, 3, 4];
592 /// assert_eq!(arr[ .. ], [0,1,2,3,4]);
593 /// assert_eq!(arr[ .. 3], [0,1,2 ]);
594 /// assert_eq!(arr[ ..=3], [0,1,2,3 ]); // RangeToInclusive
595 /// assert_eq!(arr[1.. ], [ 1,2,3,4]);
596 /// assert_eq!(arr[1.. 3], [ 1,2 ]);
597 /// assert_eq!(arr[1..=3], [ 1,2,3 ]);
600 /// [`IntoIterator`]: ../iter/trait.Iterator.html
601 /// [`Iterator`]: ../iter/trait.IntoIterator.html
602 /// [slicing index]: ../slice/trait.SliceIndex.html
603 #[doc(alias = "..=")]
604 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
605 #[stable(feature = "inclusive_range", since = "1.26.0")]
606 pub struct RangeToInclusive<Idx> {
607 /// The upper bound of the range (inclusive)
608 #[stable(feature = "inclusive_range", since = "1.26.0")]
612 #[stable(feature = "inclusive_range", since = "1.26.0")]
613 impl<Idx: fmt::Debug> fmt::Debug for RangeToInclusive<Idx> {
614 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
621 impl<Idx: PartialOrd<Idx>> RangeToInclusive<Idx> {
622 /// Returns `true` if `item` is contained in the range.
629 /// assert!( (..=5).contains(&-1_000_000_000));
630 /// assert!( (..=5).contains(&5));
631 /// assert!(!(..=5).contains(&6));
633 /// assert!( (..=1.0).contains(&1.0));
634 /// assert!(!(..=1.0).contains(&f32::NAN));
635 /// assert!(!(..=f32::NAN).contains(&0.5));
637 #[stable(feature = "range_contains", since = "1.35.0")]
638 pub fn contains<U>(&self, item: &U) -> bool
641 U: ?Sized + PartialOrd<Idx>,
643 <Self as RangeBounds<Idx>>::contains(self, item)
647 // RangeToInclusive<Idx> cannot impl From<RangeTo<Idx>>
648 // because underflow would be possible with (..0).into()
650 /// An endpoint of a range of keys.
654 /// `Bound`s are range endpoints:
657 /// use std::ops::Bound::*;
658 /// use std::ops::RangeBounds;
660 /// assert_eq!((..100).start_bound(), Unbounded);
661 /// assert_eq!((1..12).start_bound(), Included(&1));
662 /// assert_eq!((1..12).end_bound(), Excluded(&12));
665 /// Using a tuple of `Bound`s as an argument to [`BTreeMap::range`].
666 /// Note that in most cases, it's better to use range syntax (`1..5`) instead.
669 /// use std::collections::BTreeMap;
670 /// use std::ops::Bound::{Excluded, Included, Unbounded};
672 /// let mut map = BTreeMap::new();
673 /// map.insert(3, "a");
674 /// map.insert(5, "b");
675 /// map.insert(8, "c");
677 /// for (key, value) in map.range((Excluded(3), Included(8))) {
678 /// println!("{}: {}", key, value);
681 /// assert_eq!(Some((&3, &"a")), map.range((Unbounded, Included(5))).next());
684 /// [`BTreeMap::range`]: ../../std/collections/btree_map/struct.BTreeMap.html#method.range
685 #[stable(feature = "collections_bound", since = "1.17.0")]
686 #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
688 /// An inclusive bound.
689 #[stable(feature = "collections_bound", since = "1.17.0")]
690 Included(#[stable(feature = "collections_bound", since = "1.17.0")] T),
691 /// An exclusive bound.
692 #[stable(feature = "collections_bound", since = "1.17.0")]
693 Excluded(#[stable(feature = "collections_bound", since = "1.17.0")] T),
694 /// An infinite endpoint. Indicates that there is no bound in this direction.
695 #[stable(feature = "collections_bound", since = "1.17.0")]
699 impl<T: Clone> Bound<&T> {
700 /// Map a `Bound<&T>` to a `Bound<T>` by cloning the contents of the bound.
705 /// use std::ops::Bound::*;
706 /// use std::ops::RangeBounds;
708 /// assert_eq!((1..12).start_bound(), Included(&1));
709 /// assert_eq!((1..12).start_bound().cloned(), Included(1));
711 #[unstable(feature = "bound_cloned", issue = "61356")]
712 pub fn cloned(&self) -> Bound<T> {
714 Bound::Unbounded => Bound::Unbounded,
715 Bound::Included(x) => Bound::Included(x.clone()),
716 Bound::Excluded(x) => Bound::Excluded(x.clone()),
721 #[stable(feature = "collections_range", since = "1.28.0")]
722 /// `RangeBounds` is implemented by Rust's built-in range types, produced
723 /// by range syntax like `..`, `a..`, `..b`, `..=c`, `d..e`, or `f..=g`.
724 pub trait RangeBounds<T: ?Sized> {
725 /// Start index bound.
727 /// Returns the start value as a `Bound`.
733 /// use std::ops::Bound::*;
734 /// use std::ops::RangeBounds;
736 /// assert_eq!((..10).start_bound(), Unbounded);
737 /// assert_eq!((3..10).start_bound(), Included(&3));
740 #[stable(feature = "collections_range", since = "1.28.0")]
741 fn start_bound(&self) -> Bound<&T>;
745 /// Returns the end value as a `Bound`.
751 /// use std::ops::Bound::*;
752 /// use std::ops::RangeBounds;
754 /// assert_eq!((3..).end_bound(), Unbounded);
755 /// assert_eq!((3..10).end_bound(), Excluded(&10));
758 #[stable(feature = "collections_range", since = "1.28.0")]
759 fn end_bound(&self) -> Bound<&T>;
761 /// Returns `true` if `item` is contained in the range.
768 /// assert!( (3..5).contains(&4));
769 /// assert!(!(3..5).contains(&2));
771 /// assert!( (0.0..1.0).contains(&0.5));
772 /// assert!(!(0.0..1.0).contains(&f32::NAN));
773 /// assert!(!(0.0..f32::NAN).contains(&0.5));
774 /// assert!(!(f32::NAN..1.0).contains(&0.5));
775 #[stable(feature = "range_contains", since = "1.35.0")]
776 fn contains<U>(&self, item: &U) -> bool
779 U: ?Sized + PartialOrd<T>,
781 (match self.start_bound() {
782 Included(ref start) => *start <= item,
783 Excluded(ref start) => *start < item,
785 }) && (match self.end_bound() {
786 Included(ref end) => item <= *end,
787 Excluded(ref end) => item < *end,
793 use self::Bound::{Excluded, Included, Unbounded};
795 #[stable(feature = "collections_range", since = "1.28.0")]
796 impl<T: ?Sized> RangeBounds<T> for RangeFull {
797 fn start_bound(&self) -> Bound<&T> {
800 fn end_bound(&self) -> Bound<&T> {
805 #[stable(feature = "collections_range", since = "1.28.0")]
806 impl<T> RangeBounds<T> for RangeFrom<T> {
807 fn start_bound(&self) -> Bound<&T> {
808 Included(&self.start)
810 fn end_bound(&self) -> Bound<&T> {
815 #[stable(feature = "collections_range", since = "1.28.0")]
816 impl<T> RangeBounds<T> for RangeTo<T> {
817 fn start_bound(&self) -> Bound<&T> {
820 fn end_bound(&self) -> Bound<&T> {
825 #[stable(feature = "collections_range", since = "1.28.0")]
826 impl<T> RangeBounds<T> for Range<T> {
827 fn start_bound(&self) -> Bound<&T> {
828 Included(&self.start)
830 fn end_bound(&self) -> Bound<&T> {
835 #[stable(feature = "collections_range", since = "1.28.0")]
836 impl<T> RangeBounds<T> for RangeInclusive<T> {
837 fn start_bound(&self) -> Bound<&T> {
838 Included(&self.start)
840 fn end_bound(&self) -> Bound<&T> {
845 #[stable(feature = "collections_range", since = "1.28.0")]
846 impl<T> RangeBounds<T> for RangeToInclusive<T> {
847 fn start_bound(&self) -> Bound<&T> {
850 fn end_bound(&self) -> Bound<&T> {
855 #[stable(feature = "collections_range", since = "1.28.0")]
856 impl<T> RangeBounds<T> for (Bound<T>, Bound<T>) {
857 fn start_bound(&self) -> Bound<&T> {
859 (Included(ref start), _) => Included(start),
860 (Excluded(ref start), _) => Excluded(start),
861 (Unbounded, _) => Unbounded,
865 fn end_bound(&self) -> Bound<&T> {
867 (_, Included(ref end)) => Included(end),
868 (_, Excluded(ref end)) => Excluded(end),
869 (_, Unbounded) => Unbounded,
874 #[stable(feature = "collections_range", since = "1.28.0")]
875 impl<'a, T: ?Sized + 'a> RangeBounds<T> for (Bound<&'a T>, Bound<&'a T>) {
876 fn start_bound(&self) -> Bound<&T> {
880 fn end_bound(&self) -> Bound<&T> {
885 #[stable(feature = "collections_range", since = "1.28.0")]
886 impl<T> RangeBounds<T> for RangeFrom<&T> {
887 fn start_bound(&self) -> Bound<&T> {
890 fn end_bound(&self) -> Bound<&T> {
895 #[stable(feature = "collections_range", since = "1.28.0")]
896 impl<T> RangeBounds<T> for RangeTo<&T> {
897 fn start_bound(&self) -> Bound<&T> {
900 fn end_bound(&self) -> Bound<&T> {
905 #[stable(feature = "collections_range", since = "1.28.0")]
906 impl<T> RangeBounds<T> for Range<&T> {
907 fn start_bound(&self) -> Bound<&T> {
910 fn end_bound(&self) -> Bound<&T> {
915 #[stable(feature = "collections_range", since = "1.28.0")]
916 impl<T> RangeBounds<T> for RangeInclusive<&T> {
917 fn start_bound(&self) -> Bound<&T> {
920 fn end_bound(&self) -> Bound<&T> {
925 #[stable(feature = "collections_range", since = "1.28.0")]
926 impl<T> RangeBounds<T> for RangeToInclusive<&T> {
927 fn start_bound(&self) -> Bound<&T> {
930 fn end_bound(&self) -> Bound<&T> {