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,
345 // - `false` upon construction
346 // - `false` when iteration has yielded an element and the iterator is not exhausted
347 // - `true` when iteration has been used to exhaust the iterator
349 // This is required to support PartialEq and Hash without a PartialOrd bound or specialization.
350 pub(crate) exhausted: bool,
353 #[stable(feature = "inclusive_range", since = "1.26.0")]
354 impl<Idx: PartialEq> PartialEq for RangeInclusive<Idx> {
356 fn eq(&self, other: &Self) -> bool {
357 self.start == other.start && self.end == other.end && self.exhausted == other.exhausted
361 #[stable(feature = "inclusive_range", since = "1.26.0")]
362 impl<Idx: Eq> Eq for RangeInclusive<Idx> {}
364 #[stable(feature = "inclusive_range", since = "1.26.0")]
365 impl<Idx: Hash> Hash for RangeInclusive<Idx> {
366 fn hash<H: Hasher>(&self, state: &mut H) {
367 self.start.hash(state);
368 self.end.hash(state);
369 self.exhausted.hash(state);
373 impl<Idx> RangeInclusive<Idx> {
374 /// Creates a new inclusive range. Equivalent to writing `start..=end`.
379 /// use std::ops::RangeInclusive;
381 /// assert_eq!(3..=5, RangeInclusive::new(3, 5));
383 #[stable(feature = "inclusive_range_methods", since = "1.27.0")]
386 #[rustc_const_stable(feature = "const_range_new", since = "1.32.0")]
387 pub const fn new(start: Idx, end: Idx) -> Self {
388 Self { start, end, exhausted: false }
391 /// Returns the lower bound of the range (inclusive).
393 /// When using an inclusive range for iteration, the values of `start()` and
394 /// [`end()`] are unspecified after the iteration ended. To determine
395 /// whether the inclusive range is empty, use the [`is_empty()`] method
396 /// instead of comparing `start() > end()`.
398 /// Note: the value returned by this method is unspecified after the range
399 /// has been iterated to exhaustion.
401 /// [`end()`]: #method.end
402 /// [`is_empty()`]: #method.is_empty
407 /// assert_eq!((3..=5).start(), &3);
409 #[stable(feature = "inclusive_range_methods", since = "1.27.0")]
410 #[rustc_const_stable(feature = "const_inclusive_range_methods", since = "1.32.0")]
412 pub const fn start(&self) -> &Idx {
416 /// Returns the upper bound of the range (inclusive).
418 /// When using an inclusive range for iteration, the values of [`start()`]
419 /// and `end()` are unspecified after the iteration ended. To determine
420 /// whether the inclusive range is empty, use the [`is_empty()`] method
421 /// instead of comparing `start() > end()`.
423 /// Note: the value returned by this method is unspecified after the range
424 /// has been iterated to exhaustion.
426 /// [`start()`]: #method.start
427 /// [`is_empty()`]: #method.is_empty
432 /// assert_eq!((3..=5).end(), &5);
434 #[stable(feature = "inclusive_range_methods", since = "1.27.0")]
435 #[rustc_const_stable(feature = "const_inclusive_range_methods", since = "1.32.0")]
437 pub const fn end(&self) -> &Idx {
441 /// Destructures the `RangeInclusive` into (lower bound, upper (inclusive) bound).
443 /// Note: the value returned by this method is unspecified after the range
444 /// has been iterated to exhaustion.
449 /// assert_eq!((3..=5).into_inner(), (3, 5));
451 #[stable(feature = "inclusive_range_methods", since = "1.27.0")]
453 pub fn into_inner(self) -> (Idx, Idx) {
454 (self.start, self.end)
458 #[stable(feature = "inclusive_range", since = "1.26.0")]
459 impl<Idx: fmt::Debug> fmt::Debug for RangeInclusive<Idx> {
460 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
461 self.start.fmt(fmt)?;
465 write!(fmt, " (exhausted)")?;
471 impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
472 /// Returns `true` if `item` is contained in the range.
479 /// assert!(!(3..=5).contains(&2));
480 /// assert!( (3..=5).contains(&3));
481 /// assert!( (3..=5).contains(&4));
482 /// assert!( (3..=5).contains(&5));
483 /// assert!(!(3..=5).contains(&6));
485 /// assert!( (3..=3).contains(&3));
486 /// assert!(!(3..=2).contains(&3));
488 /// assert!( (0.0..=1.0).contains(&1.0));
489 /// assert!(!(0.0..=1.0).contains(&f32::NAN));
490 /// assert!(!(0.0..=f32::NAN).contains(&0.0));
491 /// assert!(!(f32::NAN..=1.0).contains(&1.0));
493 #[stable(feature = "range_contains", since = "1.35.0")]
494 pub fn contains<U>(&self, item: &U) -> bool
497 U: ?Sized + PartialOrd<Idx>,
499 <Self as RangeBounds<Idx>>::contains(self, item)
502 /// Returns `true` if the range contains no items.
507 /// #![feature(range_is_empty)]
509 /// assert!(!(3..=5).is_empty());
510 /// assert!(!(3..=3).is_empty());
511 /// assert!( (3..=2).is_empty());
514 /// The range is empty if either side is incomparable:
517 /// #![feature(range_is_empty)]
519 /// use std::f32::NAN;
520 /// assert!(!(3.0..=5.0).is_empty());
521 /// assert!( (3.0..=NAN).is_empty());
522 /// assert!( (NAN..=5.0).is_empty());
525 /// This method returns `true` after iteration has finished:
528 /// #![feature(range_is_empty)]
530 /// let mut r = 3..=5;
531 /// for _ in r.by_ref() {}
532 /// // Precise field values are unspecified here
533 /// assert!(r.is_empty());
535 #[unstable(feature = "range_is_empty", reason = "recently added", issue = "48111")]
537 pub fn is_empty(&self) -> bool {
538 self.exhausted || !(self.start <= self.end)
542 /// A range only bounded inclusively above (`..=end`).
544 /// The `RangeToInclusive` `..=end` contains all values with `x <= end`.
545 /// It cannot serve as an [`Iterator`] because it doesn't have a starting point.
549 /// The `..=end` syntax is a `RangeToInclusive`:
552 /// assert_eq!((..=5), std::ops::RangeToInclusive{ end: 5 });
555 /// It does not have an [`IntoIterator`] implementation, so you can't use it in a
556 /// `for` loop directly. This won't compile:
558 /// ```compile_fail,E0277
559 /// // error[E0277]: the trait bound `std::ops::RangeToInclusive<{integer}>:
560 /// // std::iter::Iterator` is not satisfied
566 /// When used as a [slicing index], `RangeToInclusive` produces a slice of all
567 /// array elements up to and including the index indicated by `end`.
570 /// let arr = [0, 1, 2, 3, 4];
571 /// assert_eq!(arr[ .. ], [0,1,2,3,4]);
572 /// assert_eq!(arr[ .. 3], [0,1,2 ]);
573 /// assert_eq!(arr[ ..=3], [0,1,2,3 ]); // RangeToInclusive
574 /// assert_eq!(arr[1.. ], [ 1,2,3,4]);
575 /// assert_eq!(arr[1.. 3], [ 1,2 ]);
576 /// assert_eq!(arr[1..=3], [ 1,2,3 ]);
579 /// [`IntoIterator`]: ../iter/trait.Iterator.html
580 /// [`Iterator`]: ../iter/trait.IntoIterator.html
581 /// [slicing index]: ../slice/trait.SliceIndex.html
582 #[doc(alias = "..=")]
583 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
584 #[stable(feature = "inclusive_range", since = "1.26.0")]
585 pub struct RangeToInclusive<Idx> {
586 /// The upper bound of the range (inclusive)
587 #[stable(feature = "inclusive_range", since = "1.26.0")]
591 #[stable(feature = "inclusive_range", since = "1.26.0")]
592 impl<Idx: fmt::Debug> fmt::Debug for RangeToInclusive<Idx> {
593 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
600 impl<Idx: PartialOrd<Idx>> RangeToInclusive<Idx> {
601 /// Returns `true` if `item` is contained in the range.
608 /// assert!( (..=5).contains(&-1_000_000_000));
609 /// assert!( (..=5).contains(&5));
610 /// assert!(!(..=5).contains(&6));
612 /// assert!( (..=1.0).contains(&1.0));
613 /// assert!(!(..=1.0).contains(&f32::NAN));
614 /// assert!(!(..=f32::NAN).contains(&0.5));
616 #[stable(feature = "range_contains", since = "1.35.0")]
617 pub fn contains<U>(&self, item: &U) -> bool
620 U: ?Sized + PartialOrd<Idx>,
622 <Self as RangeBounds<Idx>>::contains(self, item)
626 // RangeToInclusive<Idx> cannot impl From<RangeTo<Idx>>
627 // because underflow would be possible with (..0).into()
629 /// An endpoint of a range of keys.
633 /// `Bound`s are range endpoints:
636 /// use std::ops::Bound::*;
637 /// use std::ops::RangeBounds;
639 /// assert_eq!((..100).start_bound(), Unbounded);
640 /// assert_eq!((1..12).start_bound(), Included(&1));
641 /// assert_eq!((1..12).end_bound(), Excluded(&12));
644 /// Using a tuple of `Bound`s as an argument to [`BTreeMap::range`].
645 /// Note that in most cases, it's better to use range syntax (`1..5`) instead.
648 /// use std::collections::BTreeMap;
649 /// use std::ops::Bound::{Excluded, Included, Unbounded};
651 /// let mut map = BTreeMap::new();
652 /// map.insert(3, "a");
653 /// map.insert(5, "b");
654 /// map.insert(8, "c");
656 /// for (key, value) in map.range((Excluded(3), Included(8))) {
657 /// println!("{}: {}", key, value);
660 /// assert_eq!(Some((&3, &"a")), map.range((Unbounded, Included(5))).next());
663 /// [`BTreeMap::range`]: ../../std/collections/btree_map/struct.BTreeMap.html#method.range
664 #[stable(feature = "collections_bound", since = "1.17.0")]
665 #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
667 /// An inclusive bound.
668 #[stable(feature = "collections_bound", since = "1.17.0")]
669 Included(#[stable(feature = "collections_bound", since = "1.17.0")] T),
670 /// An exclusive bound.
671 #[stable(feature = "collections_bound", since = "1.17.0")]
672 Excluded(#[stable(feature = "collections_bound", since = "1.17.0")] T),
673 /// An infinite endpoint. Indicates that there is no bound in this direction.
674 #[stable(feature = "collections_bound", since = "1.17.0")]
678 impl<T: Clone> Bound<&T> {
679 /// Map a `Bound<&T>` to a `Bound<T>` by cloning the contents of the bound.
684 /// #![feature(bound_cloned)]
685 /// use std::ops::Bound::*;
686 /// use std::ops::RangeBounds;
688 /// assert_eq!((1..12).start_bound(), Included(&1));
689 /// assert_eq!((1..12).start_bound().cloned(), Included(1));
691 #[unstable(feature = "bound_cloned", issue = "61356")]
692 pub fn cloned(self) -> Bound<T> {
694 Bound::Unbounded => Bound::Unbounded,
695 Bound::Included(x) => Bound::Included(x.clone()),
696 Bound::Excluded(x) => Bound::Excluded(x.clone()),
701 #[stable(feature = "collections_range", since = "1.28.0")]
702 /// `RangeBounds` is implemented by Rust's built-in range types, produced
703 /// by range syntax like `..`, `a..`, `..b`, `..=c`, `d..e`, or `f..=g`.
704 pub trait RangeBounds<T: ?Sized> {
705 /// Start index bound.
707 /// Returns the start value as a `Bound`.
713 /// use std::ops::Bound::*;
714 /// use std::ops::RangeBounds;
716 /// assert_eq!((..10).start_bound(), Unbounded);
717 /// assert_eq!((3..10).start_bound(), Included(&3));
720 #[stable(feature = "collections_range", since = "1.28.0")]
721 fn start_bound(&self) -> Bound<&T>;
725 /// Returns the end value as a `Bound`.
731 /// use std::ops::Bound::*;
732 /// use std::ops::RangeBounds;
734 /// assert_eq!((3..).end_bound(), Unbounded);
735 /// assert_eq!((3..10).end_bound(), Excluded(&10));
738 #[stable(feature = "collections_range", since = "1.28.0")]
739 fn end_bound(&self) -> Bound<&T>;
741 /// Returns `true` if `item` is contained in the range.
748 /// assert!( (3..5).contains(&4));
749 /// assert!(!(3..5).contains(&2));
751 /// assert!( (0.0..1.0).contains(&0.5));
752 /// assert!(!(0.0..1.0).contains(&f32::NAN));
753 /// assert!(!(0.0..f32::NAN).contains(&0.5));
754 /// assert!(!(f32::NAN..1.0).contains(&0.5));
755 #[stable(feature = "range_contains", since = "1.35.0")]
756 fn contains<U>(&self, item: &U) -> bool
759 U: ?Sized + PartialOrd<T>,
761 (match self.start_bound() {
762 Included(ref start) => *start <= item,
763 Excluded(ref start) => *start < item,
765 }) && (match self.end_bound() {
766 Included(ref end) => item <= *end,
767 Excluded(ref end) => item < *end,
773 use self::Bound::{Excluded, Included, Unbounded};
775 #[stable(feature = "collections_range", since = "1.28.0")]
776 impl<T: ?Sized> RangeBounds<T> for RangeFull {
777 fn start_bound(&self) -> Bound<&T> {
780 fn end_bound(&self) -> Bound<&T> {
785 #[stable(feature = "collections_range", since = "1.28.0")]
786 impl<T> RangeBounds<T> for RangeFrom<T> {
787 fn start_bound(&self) -> Bound<&T> {
788 Included(&self.start)
790 fn end_bound(&self) -> Bound<&T> {
795 #[stable(feature = "collections_range", since = "1.28.0")]
796 impl<T> RangeBounds<T> for RangeTo<T> {
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 Range<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 RangeInclusive<T> {
817 fn start_bound(&self) -> Bound<&T> {
818 Included(&self.start)
820 fn end_bound(&self) -> Bound<&T> {
825 #[stable(feature = "collections_range", since = "1.28.0")]
826 impl<T> RangeBounds<T> for RangeToInclusive<T> {
827 fn start_bound(&self) -> Bound<&T> {
830 fn end_bound(&self) -> Bound<&T> {
835 #[stable(feature = "collections_range", since = "1.28.0")]
836 impl<T> RangeBounds<T> for (Bound<T>, Bound<T>) {
837 fn start_bound(&self) -> Bound<&T> {
839 (Included(ref start), _) => Included(start),
840 (Excluded(ref start), _) => Excluded(start),
841 (Unbounded, _) => Unbounded,
845 fn end_bound(&self) -> Bound<&T> {
847 (_, Included(ref end)) => Included(end),
848 (_, Excluded(ref end)) => Excluded(end),
849 (_, Unbounded) => Unbounded,
854 #[stable(feature = "collections_range", since = "1.28.0")]
855 impl<'a, T: ?Sized + 'a> RangeBounds<T> for (Bound<&'a T>, Bound<&'a T>) {
856 fn start_bound(&self) -> Bound<&T> {
860 fn end_bound(&self) -> Bound<&T> {
865 #[stable(feature = "collections_range", since = "1.28.0")]
866 impl<T> RangeBounds<T> for RangeFrom<&T> {
867 fn start_bound(&self) -> Bound<&T> {
870 fn end_bound(&self) -> Bound<&T> {
875 #[stable(feature = "collections_range", since = "1.28.0")]
876 impl<T> RangeBounds<T> for RangeTo<&T> {
877 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 Range<&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 RangeInclusive<&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 RangeToInclusive<&T> {
907 fn start_bound(&self) -> Bound<&T> {
910 fn end_bound(&self) -> Bound<&T> {