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]); // This is the `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 /// [slicing index]: crate::slice::SliceIndex
41 #[derive(Copy, Clone, Default, PartialEq, Eq, Hash)]
42 #[stable(feature = "rust1", since = "1.0.0")]
45 #[stable(feature = "rust1", since = "1.0.0")]
46 impl fmt::Debug for RangeFull {
47 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
52 /// A (half-open) range bounded inclusively below and exclusively above
55 /// The range `start..end` contains all values with `start <= x < end`.
56 /// It is empty if `start >= end`.
60 /// The `start..end` syntax is a `Range`:
63 /// assert_eq!((3..5), std::ops::Range { start: 3, end: 5 });
64 /// assert_eq!(3 + 4 + 5, (3..6).sum());
68 /// let arr = [0, 1, 2, 3, 4];
69 /// assert_eq!(arr[ .. ], [0, 1, 2, 3, 4]);
70 /// assert_eq!(arr[ .. 3], [0, 1, 2 ]);
71 /// assert_eq!(arr[ ..=3], [0, 1, 2, 3 ]);
72 /// assert_eq!(arr[1.. ], [ 1, 2, 3, 4]);
73 /// assert_eq!(arr[1.. 3], [ 1, 2 ]); // This is a `Range`
74 /// assert_eq!(arr[1..=3], [ 1, 2, 3 ]);
78 #[derive(Clone, Default, PartialEq, Eq, Hash)] // not Copy -- see #27186
79 #[stable(feature = "rust1", since = "1.0.0")]
80 pub struct Range<Idx> {
81 /// The lower bound of the range (inclusive).
82 #[stable(feature = "rust1", since = "1.0.0")]
84 /// The upper bound of the range (exclusive).
85 #[stable(feature = "rust1", since = "1.0.0")]
89 #[stable(feature = "rust1", since = "1.0.0")]
90 impl<Idx: fmt::Debug> fmt::Debug for Range<Idx> {
91 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
99 impl<Idx: PartialOrd<Idx>> Range<Idx> {
100 /// Returns `true` if `item` is contained in the range.
105 /// assert!(!(3..5).contains(&2));
106 /// assert!( (3..5).contains(&3));
107 /// assert!( (3..5).contains(&4));
108 /// assert!(!(3..5).contains(&5));
110 /// assert!(!(3..3).contains(&3));
111 /// assert!(!(3..2).contains(&3));
113 /// assert!( (0.0..1.0).contains(&0.5));
114 /// assert!(!(0.0..1.0).contains(&f32::NAN));
115 /// assert!(!(0.0..f32::NAN).contains(&0.5));
116 /// assert!(!(f32::NAN..1.0).contains(&0.5));
118 #[stable(feature = "range_contains", since = "1.35.0")]
119 pub fn contains<U>(&self, item: &U) -> bool
122 U: ?Sized + PartialOrd<Idx>,
124 <Self as RangeBounds<Idx>>::contains(self, item)
127 /// Returns `true` if the range contains no items.
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 /// assert!(!(3.0..5.0).is_empty());
141 /// assert!( (3.0..f32::NAN).is_empty());
142 /// assert!( (f32::NAN..5.0).is_empty());
144 #[stable(feature = "range_is_empty", since = "1.47.0")]
145 pub fn is_empty(&self) -> bool {
146 !(self.start < self.end)
150 /// A range only bounded inclusively below (`start..`).
152 /// The `RangeFrom` `start..` contains all values with `x >= start`.
154 /// *Note*: Overflow in the [`Iterator`] implementation (when the contained
155 /// data type reaches its numerical limit) is allowed to panic, wrap, or
156 /// saturate. This behavior is defined by the implementation of the [`Step`]
157 /// trait. For primitive integers, this follows the normal rules, and respects
158 /// the overflow checks profile (panic in debug, wrap in release). Note also
159 /// that overflow happens earlier than you might assume: the overflow happens
160 /// in the call to `next` that yields the maximum value, as the range must be
161 /// set to a state to yield the next value.
163 /// [`Step`]: crate::iter::Step
167 /// The `start..` syntax is a `RangeFrom`:
170 /// assert_eq!((2..), std::ops::RangeFrom { start: 2 });
171 /// assert_eq!(2 + 3 + 4, (2..).take(3).sum());
175 /// let arr = [0, 1, 2, 3, 4];
176 /// assert_eq!(arr[ .. ], [0, 1, 2, 3, 4]);
177 /// assert_eq!(arr[ .. 3], [0, 1, 2 ]);
178 /// assert_eq!(arr[ ..=3], [0, 1, 2, 3 ]);
179 /// assert_eq!(arr[1.. ], [ 1, 2, 3, 4]); // This is a `RangeFrom`
180 /// assert_eq!(arr[1.. 3], [ 1, 2 ]);
181 /// assert_eq!(arr[1..=3], [ 1, 2, 3 ]);
183 #[lang = "RangeFrom"]
185 #[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186
186 #[stable(feature = "rust1", since = "1.0.0")]
187 pub struct RangeFrom<Idx> {
188 /// The lower bound of the range (inclusive).
189 #[stable(feature = "rust1", since = "1.0.0")]
193 #[stable(feature = "rust1", since = "1.0.0")]
194 impl<Idx: fmt::Debug> fmt::Debug for RangeFrom<Idx> {
195 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
196 self.start.fmt(fmt)?;
202 impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
203 /// Returns `true` if `item` is contained in the range.
208 /// assert!(!(3..).contains(&2));
209 /// assert!( (3..).contains(&3));
210 /// assert!( (3..).contains(&1_000_000_000));
212 /// assert!( (0.0..).contains(&0.5));
213 /// assert!(!(0.0..).contains(&f32::NAN));
214 /// assert!(!(f32::NAN..).contains(&0.5));
216 #[stable(feature = "range_contains", since = "1.35.0")]
217 pub fn contains<U>(&self, item: &U) -> bool
220 U: ?Sized + PartialOrd<Idx>,
222 <Self as RangeBounds<Idx>>::contains(self, item)
226 /// A range only bounded exclusively above (`..end`).
228 /// The `RangeTo` `..end` contains all values with `x < end`.
229 /// It cannot serve as an [`Iterator`] because it doesn't have a starting point.
233 /// The `..end` syntax is a `RangeTo`:
236 /// assert_eq!((..5), std::ops::RangeTo { end: 5 });
239 /// It does not have an [`IntoIterator`] implementation, so you can't use it in
240 /// a `for` loop directly. This won't compile:
242 /// ```compile_fail,E0277
243 /// // error[E0277]: the trait bound `std::ops::RangeTo<{integer}>:
244 /// // std::iter::Iterator` is not satisfied
250 /// When used as a [slicing index], `RangeTo` produces a slice of all array
251 /// elements before the index indicated by `end`.
254 /// let arr = [0, 1, 2, 3, 4];
255 /// assert_eq!(arr[ .. ], [0, 1, 2, 3, 4]);
256 /// assert_eq!(arr[ .. 3], [0, 1, 2 ]); // This is a `RangeTo`
257 /// assert_eq!(arr[ ..=3], [0, 1, 2, 3 ]);
258 /// assert_eq!(arr[1.. ], [ 1, 2, 3, 4]);
259 /// assert_eq!(arr[1.. 3], [ 1, 2 ]);
260 /// assert_eq!(arr[1..=3], [ 1, 2, 3 ]);
263 /// [slicing index]: crate::slice::SliceIndex
266 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
267 #[stable(feature = "rust1", since = "1.0.0")]
268 pub struct RangeTo<Idx> {
269 /// The upper bound of the range (exclusive).
270 #[stable(feature = "rust1", since = "1.0.0")]
274 #[stable(feature = "rust1", since = "1.0.0")]
275 impl<Idx: fmt::Debug> fmt::Debug for RangeTo<Idx> {
276 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
283 impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
284 /// Returns `true` if `item` is contained in the range.
289 /// assert!( (..5).contains(&-1_000_000_000));
290 /// assert!( (..5).contains(&4));
291 /// assert!(!(..5).contains(&5));
293 /// assert!( (..1.0).contains(&0.5));
294 /// assert!(!(..1.0).contains(&f32::NAN));
295 /// assert!(!(..f32::NAN).contains(&0.5));
297 #[stable(feature = "range_contains", since = "1.35.0")]
298 pub fn contains<U>(&self, item: &U) -> bool
301 U: ?Sized + PartialOrd<Idx>,
303 <Self as RangeBounds<Idx>>::contains(self, item)
307 /// A range bounded inclusively below and above (`start..=end`).
309 /// The `RangeInclusive` `start..=end` contains all values with `x >= start`
310 /// and `x <= end`. It is empty unless `start <= end`.
312 /// This iterator is [fused], but the specific values of `start` and `end` after
313 /// iteration has finished are **unspecified** other than that [`.is_empty()`]
314 /// will return `true` once no more values will be produced.
316 /// [fused]: crate::iter::FusedIterator
317 /// [`.is_empty()`]: RangeInclusive::is_empty
321 /// The `start..=end` syntax is a `RangeInclusive`:
324 /// assert_eq!((3..=5), std::ops::RangeInclusive::new(3, 5));
325 /// assert_eq!(3 + 4 + 5, (3..=5).sum());
329 /// let arr = [0, 1, 2, 3, 4];
330 /// assert_eq!(arr[ .. ], [0, 1, 2, 3, 4]);
331 /// assert_eq!(arr[ .. 3], [0, 1, 2 ]);
332 /// assert_eq!(arr[ ..=3], [0, 1, 2, 3 ]);
333 /// assert_eq!(arr[1.. ], [ 1, 2, 3, 4]);
334 /// assert_eq!(arr[1.. 3], [ 1, 2 ]);
335 /// assert_eq!(arr[1..=3], [ 1, 2, 3 ]); // This is a `RangeInclusive`
337 #[lang = "RangeInclusive"]
338 #[doc(alias = "..=")]
339 #[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186
340 #[stable(feature = "inclusive_range", since = "1.26.0")]
341 pub struct RangeInclusive<Idx> {
342 // Note that the fields here are not public to allow changing the
343 // representation in the future; in particular, while we could plausibly
344 // expose start/end, modifying them without changing (future/current)
345 // private fields may lead to incorrect behavior, so we don't want to
346 // support that mode.
347 pub(crate) start: Idx,
351 // - `false` upon construction
352 // - `false` when iteration has yielded an element and the iterator is not exhausted
353 // - `true` when iteration has been used to exhaust the iterator
355 // This is required to support PartialEq and Hash without a PartialOrd bound or specialization.
356 pub(crate) exhausted: bool,
359 impl<Idx> RangeInclusive<Idx> {
360 /// Creates a new inclusive range. Equivalent to writing `start..=end`.
365 /// use std::ops::RangeInclusive;
367 /// assert_eq!(3..=5, RangeInclusive::new(3, 5));
369 #[lang = "range_inclusive_new"]
370 #[stable(feature = "inclusive_range_methods", since = "1.27.0")]
373 #[rustc_const_stable(feature = "const_range_new", since = "1.32.0")]
374 pub const fn new(start: Idx, end: Idx) -> Self {
375 Self { start, end, exhausted: false }
378 /// Returns the lower bound of the range (inclusive).
380 /// When using an inclusive range for iteration, the values of `start()` and
381 /// [`end()`] are unspecified after the iteration ended. To determine
382 /// whether the inclusive range is empty, use the [`is_empty()`] method
383 /// instead of comparing `start() > end()`.
385 /// Note: the value returned by this method is unspecified after the range
386 /// has been iterated to exhaustion.
388 /// [`end()`]: RangeInclusive::end
389 /// [`is_empty()`]: RangeInclusive::is_empty
394 /// assert_eq!((3..=5).start(), &3);
396 #[stable(feature = "inclusive_range_methods", since = "1.27.0")]
397 #[rustc_const_stable(feature = "const_inclusive_range_methods", since = "1.32.0")]
399 pub const fn start(&self) -> &Idx {
403 /// Returns the upper bound of the range (inclusive).
405 /// When using an inclusive range for iteration, the values of [`start()`]
406 /// and `end()` are unspecified after the iteration ended. To determine
407 /// whether the inclusive range is empty, use the [`is_empty()`] method
408 /// instead of comparing `start() > end()`.
410 /// Note: the value returned by this method is unspecified after the range
411 /// has been iterated to exhaustion.
413 /// [`start()`]: RangeInclusive::start
414 /// [`is_empty()`]: RangeInclusive::is_empty
419 /// assert_eq!((3..=5).end(), &5);
421 #[stable(feature = "inclusive_range_methods", since = "1.27.0")]
422 #[rustc_const_stable(feature = "const_inclusive_range_methods", since = "1.32.0")]
424 pub const fn end(&self) -> &Idx {
428 /// Destructures the `RangeInclusive` into (lower bound, upper (inclusive) bound).
430 /// Note: the value returned by this method is unspecified after the range
431 /// has been iterated to exhaustion.
436 /// assert_eq!((3..=5).into_inner(), (3, 5));
438 #[stable(feature = "inclusive_range_methods", since = "1.27.0")]
440 pub fn into_inner(self) -> (Idx, Idx) {
441 (self.start, self.end)
445 impl RangeInclusive<usize> {
446 /// Converts to an exclusive `Range` for `SliceIndex` implementations.
447 /// The caller is responsible for dealing with `end == usize::MAX`.
449 pub(crate) fn into_slice_range(self) -> Range<usize> {
450 // If we're not exhausted, we want to simply slice `start..end + 1`.
451 // If we are exhausted, then slicing with `end + 1..end + 1` gives us an
452 // empty range that is still subject to bounds-checks for that endpoint.
453 let exclusive_end = self.end + 1;
454 let start = if self.exhausted { exclusive_end } else { self.start };
459 #[stable(feature = "inclusive_range", since = "1.26.0")]
460 impl<Idx: fmt::Debug> fmt::Debug for RangeInclusive<Idx> {
461 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
462 self.start.fmt(fmt)?;
466 write!(fmt, " (exhausted)")?;
472 impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
473 /// Returns `true` if `item` is contained in the range.
478 /// assert!(!(3..=5).contains(&2));
479 /// assert!( (3..=5).contains(&3));
480 /// assert!( (3..=5).contains(&4));
481 /// assert!( (3..=5).contains(&5));
482 /// assert!(!(3..=5).contains(&6));
484 /// assert!( (3..=3).contains(&3));
485 /// assert!(!(3..=2).contains(&3));
487 /// assert!( (0.0..=1.0).contains(&1.0));
488 /// assert!(!(0.0..=1.0).contains(&f32::NAN));
489 /// assert!(!(0.0..=f32::NAN).contains(&0.0));
490 /// assert!(!(f32::NAN..=1.0).contains(&1.0));
493 /// This method always returns `false` after iteration has finished:
496 /// let mut r = 3..=5;
497 /// assert!(r.contains(&3) && r.contains(&5));
498 /// for _ in r.by_ref() {}
499 /// // Precise field values are unspecified here
500 /// assert!(!r.contains(&3) && !r.contains(&5));
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 /// assert!(!(3..=5).is_empty());
517 /// assert!(!(3..=3).is_empty());
518 /// assert!( (3..=2).is_empty());
521 /// The range is empty if either side is incomparable:
524 /// assert!(!(3.0..=5.0).is_empty());
525 /// assert!( (3.0..=f32::NAN).is_empty());
526 /// assert!( (f32::NAN..=5.0).is_empty());
529 /// This method returns `true` after iteration has finished:
532 /// let mut r = 3..=5;
533 /// for _ in r.by_ref() {}
534 /// // Precise field values are unspecified here
535 /// assert!(r.is_empty());
537 #[stable(feature = "range_is_empty", since = "1.47.0")]
539 pub fn is_empty(&self) -> bool {
540 self.exhausted || !(self.start <= self.end)
544 /// A range only bounded inclusively above (`..=end`).
546 /// The `RangeToInclusive` `..=end` contains all values with `x <= end`.
547 /// It cannot serve as an [`Iterator`] because it doesn't have a starting point.
551 /// The `..=end` syntax is a `RangeToInclusive`:
554 /// assert_eq!((..=5), std::ops::RangeToInclusive{ end: 5 });
557 /// It does not have an [`IntoIterator`] implementation, so you can't use it in a
558 /// `for` loop directly. This won't compile:
560 /// ```compile_fail,E0277
561 /// // error[E0277]: the trait bound `std::ops::RangeToInclusive<{integer}>:
562 /// // std::iter::Iterator` is not satisfied
568 /// When used as a [slicing index], `RangeToInclusive` produces a slice of all
569 /// array elements up to and including the index indicated by `end`.
572 /// let arr = [0, 1, 2, 3, 4];
573 /// assert_eq!(arr[ .. ], [0, 1, 2, 3, 4]);
574 /// assert_eq!(arr[ .. 3], [0, 1, 2 ]);
575 /// assert_eq!(arr[ ..=3], [0, 1, 2, 3 ]); // This is a `RangeToInclusive`
576 /// assert_eq!(arr[1.. ], [ 1, 2, 3, 4]);
577 /// assert_eq!(arr[1.. 3], [ 1, 2 ]);
578 /// assert_eq!(arr[1..=3], [ 1, 2, 3 ]);
581 /// [slicing index]: crate::slice::SliceIndex
582 #[lang = "RangeToInclusive"]
583 #[doc(alias = "..=")]
584 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
585 #[stable(feature = "inclusive_range", since = "1.26.0")]
586 pub struct RangeToInclusive<Idx> {
587 /// The upper bound of the range (inclusive)
588 #[stable(feature = "inclusive_range", since = "1.26.0")]
592 #[stable(feature = "inclusive_range", since = "1.26.0")]
593 impl<Idx: fmt::Debug> fmt::Debug for RangeToInclusive<Idx> {
594 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
601 impl<Idx: PartialOrd<Idx>> RangeToInclusive<Idx> {
602 /// Returns `true` if `item` is contained in the range.
607 /// assert!( (..=5).contains(&-1_000_000_000));
608 /// assert!( (..=5).contains(&5));
609 /// assert!(!(..=5).contains(&6));
611 /// assert!( (..=1.0).contains(&1.0));
612 /// assert!(!(..=1.0).contains(&f32::NAN));
613 /// assert!(!(..=f32::NAN).contains(&0.5));
615 #[stable(feature = "range_contains", since = "1.35.0")]
616 pub fn contains<U>(&self, item: &U) -> bool
619 U: ?Sized + PartialOrd<Idx>,
621 <Self as RangeBounds<Idx>>::contains(self, item)
625 // RangeToInclusive<Idx> cannot impl From<RangeTo<Idx>>
626 // because underflow would be possible with (..0).into()
628 /// An endpoint of a range of keys.
632 /// `Bound`s are range endpoints:
635 /// use std::ops::Bound::*;
636 /// use std::ops::RangeBounds;
638 /// assert_eq!((..100).start_bound(), Unbounded);
639 /// assert_eq!((1..12).start_bound(), Included(&1));
640 /// assert_eq!((1..12).end_bound(), Excluded(&12));
643 /// Using a tuple of `Bound`s as an argument to [`BTreeMap::range`].
644 /// Note that in most cases, it's better to use range syntax (`1..5`) instead.
647 /// use std::collections::BTreeMap;
648 /// use std::ops::Bound::{Excluded, Included, Unbounded};
650 /// let mut map = BTreeMap::new();
651 /// map.insert(3, "a");
652 /// map.insert(5, "b");
653 /// map.insert(8, "c");
655 /// for (key, value) in map.range((Excluded(3), Included(8))) {
656 /// println!("{}: {}", key, value);
659 /// assert_eq!(Some((&3, &"a")), map.range((Unbounded, Included(5))).next());
662 /// [`BTreeMap::range`]: ../../std/collections/btree_map/struct.BTreeMap.html#method.range
663 #[stable(feature = "collections_bound", since = "1.17.0")]
664 #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
666 /// An inclusive bound.
667 #[stable(feature = "collections_bound", since = "1.17.0")]
668 Included(#[stable(feature = "collections_bound", since = "1.17.0")] T),
669 /// An exclusive bound.
670 #[stable(feature = "collections_bound", since = "1.17.0")]
671 Excluded(#[stable(feature = "collections_bound", since = "1.17.0")] T),
672 /// An infinite endpoint. Indicates that there is no bound in this direction.
673 #[stable(feature = "collections_bound", since = "1.17.0")]
677 #[unstable(feature = "bound_as_ref", issue = "80996")]
679 /// Converts from `&Bound<T>` to `Bound<&T>`.
681 pub fn as_ref(&self) -> Bound<&T> {
683 Included(ref x) => Included(x),
684 Excluded(ref x) => Excluded(x),
685 Unbounded => Unbounded,
689 /// Converts from `&mut Bound<T>` to `Bound<&T>`.
691 pub fn as_mut(&mut self) -> Bound<&mut T> {
693 Included(ref mut x) => Included(x),
694 Excluded(ref mut x) => Excluded(x),
695 Unbounded => Unbounded,
700 impl<T: Clone> Bound<&T> {
701 /// Map a `Bound<&T>` to a `Bound<T>` by cloning the contents of the bound.
706 /// #![feature(bound_cloned)]
707 /// use std::ops::Bound::*;
708 /// use std::ops::RangeBounds;
710 /// assert_eq!((1..12).start_bound(), Included(&1));
711 /// assert_eq!((1..12).start_bound().cloned(), Included(1));
713 #[unstable(feature = "bound_cloned", issue = "61356")]
714 pub fn cloned(self) -> Bound<T> {
716 Bound::Unbounded => Bound::Unbounded,
717 Bound::Included(x) => Bound::Included(x.clone()),
718 Bound::Excluded(x) => Bound::Excluded(x.clone()),
723 /// `RangeBounds` is implemented by Rust's built-in range types, produced
724 /// by range syntax like `..`, `a..`, `..b`, `..=c`, `d..e`, or `f..=g`.
725 #[stable(feature = "collections_range", since = "1.28.0")]
726 pub trait RangeBounds<T: ?Sized> {
727 /// Start index bound.
729 /// Returns the start value as a `Bound`.
735 /// use std::ops::Bound::*;
736 /// use std::ops::RangeBounds;
738 /// assert_eq!((..10).start_bound(), Unbounded);
739 /// assert_eq!((3..10).start_bound(), Included(&3));
742 #[stable(feature = "collections_range", since = "1.28.0")]
743 fn start_bound(&self) -> Bound<&T>;
747 /// Returns the end value as a `Bound`.
753 /// use std::ops::Bound::*;
754 /// use std::ops::RangeBounds;
756 /// assert_eq!((3..).end_bound(), Unbounded);
757 /// assert_eq!((3..10).end_bound(), Excluded(&10));
760 #[stable(feature = "collections_range", since = "1.28.0")]
761 fn end_bound(&self) -> Bound<&T>;
763 /// 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> {
842 // When the iterator is exhausted, we usually have start == end,
843 // but we want the range to appear empty, containing nothing.
851 #[stable(feature = "collections_range", since = "1.28.0")]
852 impl<T> RangeBounds<T> for RangeToInclusive<T> {
853 fn start_bound(&self) -> Bound<&T> {
856 fn end_bound(&self) -> Bound<&T> {
861 #[stable(feature = "collections_range", since = "1.28.0")]
862 impl<T> RangeBounds<T> for (Bound<T>, Bound<T>) {
863 fn start_bound(&self) -> Bound<&T> {
865 (Included(ref start), _) => Included(start),
866 (Excluded(ref start), _) => Excluded(start),
867 (Unbounded, _) => Unbounded,
871 fn end_bound(&self) -> Bound<&T> {
873 (_, Included(ref end)) => Included(end),
874 (_, Excluded(ref end)) => Excluded(end),
875 (_, Unbounded) => Unbounded,
880 #[stable(feature = "collections_range", since = "1.28.0")]
881 impl<'a, T: ?Sized + 'a> RangeBounds<T> for (Bound<&'a T>, Bound<&'a T>) {
882 fn start_bound(&self) -> Bound<&T> {
886 fn end_bound(&self) -> Bound<&T> {
891 #[stable(feature = "collections_range", since = "1.28.0")]
892 impl<T> RangeBounds<T> for RangeFrom<&T> {
893 fn start_bound(&self) -> Bound<&T> {
896 fn end_bound(&self) -> Bound<&T> {
901 #[stable(feature = "collections_range", since = "1.28.0")]
902 impl<T> RangeBounds<T> for RangeTo<&T> {
903 fn start_bound(&self) -> Bound<&T> {
906 fn end_bound(&self) -> Bound<&T> {
911 #[stable(feature = "collections_range", since = "1.28.0")]
912 impl<T> RangeBounds<T> for Range<&T> {
913 fn start_bound(&self) -> Bound<&T> {
916 fn end_bound(&self) -> Bound<&T> {
921 #[stable(feature = "collections_range", since = "1.28.0")]
922 impl<T> RangeBounds<T> for RangeInclusive<&T> {
923 fn start_bound(&self) -> Bound<&T> {
926 fn end_bound(&self) -> Bound<&T> {
931 #[stable(feature = "collections_range", since = "1.28.0")]
932 impl<T> RangeBounds<T> for RangeToInclusive<&T> {
933 fn start_bound(&self) -> Bound<&T> {
936 fn end_bound(&self) -> Bound<&T> {