Skip to main content

core/
range.rs

1//! # Replacement range types
2//!
3//! The types within this module are meant to replace the legacy `Range`,
4//! `RangeInclusive`, `RangeToInclusive` and `RangeFrom` types in a future edition.
5//!
6//! ```
7//! use core::range::{Range, RangeFrom, RangeInclusive, RangeToInclusive};
8//!
9//! let arr = [0, 1, 2, 3, 4];
10//! assert_eq!(arr[                        ..   ], [0, 1, 2, 3, 4]);
11//! assert_eq!(arr[                        .. 3 ], [0, 1, 2      ]);
12//! assert_eq!(arr[RangeToInclusive::from( ..=3)], [0, 1, 2, 3   ]);
13//! assert_eq!(arr[       RangeFrom::from(1..  )], [   1, 2, 3, 4]);
14//! assert_eq!(arr[           Range::from(1..3 )], [   1, 2      ]);
15//! assert_eq!(arr[  RangeInclusive::from(1..=3)], [   1, 2, 3   ]);
16//! ```
17
18use crate::fmt;
19use crate::hash::Hash;
20use crate::marker::Destruct;
21
22mod iter;
23
24#[stable(feature = "new_range_api_legacy", since = "1.98.0")]
25pub mod legacy;
26
27use core::ops::Bound::{self, Excluded, Included, Unbounded};
28
29#[doc(inline)]
30#[stable(feature = "new_range_from_api", since = "1.96.0")]
31pub use iter::RangeFromIter;
32#[doc(inline)]
33#[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
34pub use iter::RangeInclusiveIter;
35#[doc(inline)]
36#[stable(feature = "new_range_api", since = "1.96.0")]
37pub use iter::RangeIter;
38
39use crate::iter::Step;
40// FIXME(one_sided_range): These types should move into this module.
41// FIXME(range_into_bounds): Ditto. Also consider re-exporting `RangeBounds` and related.
42use crate::ops::{IntoBounds, OneSidedRange, OneSidedRangeBound, RangeBounds};
43#[doc(inline)]
44#[stable(feature = "new_range_api_exports", since = "1.98.0")]
45pub use crate::ops::{RangeFull, RangeTo};
46
47/// A (half-open) range bounded inclusively below and exclusively above.
48///
49/// The `Range` contains all values with `start <= x < end`.
50/// It is empty if `start >= end`.
51///
52/// Note that this type is not suited to represent all possible ranges. For example, `Range<u8>`
53/// cannot represent the range that covers all of `u8`. Use [`(Bound<T>, Bound<T>)`][Bound] if you
54/// need a type that can store an arbitrary range.
55///
56/// # Examples
57///
58/// ```
59/// use core::range::Range;
60///
61/// assert_eq!(Range::from(3..5), Range { start: 3, end: 5 });
62/// assert_eq!(3 + 4 + 5, Range::from(3..6).into_iter().sum());
63/// ```
64///
65/// # Edition notes
66///
67/// It is planned that the syntax `start..end` will construct this
68/// type in a future edition, but it does not do so today.
69#[lang = "RangeCopy"]
70#[derive(#[automatically_derived]#[stable(feature = "new_range_api", since = "1.96.0")]
impl<Idx: crate::marker::Copy> crate::marker::Copy for Range<Idx> { }Copy, #[automatically_derived]#[stable(feature = "new_range_api", since = "1.96.0")]
impl<Idx: crate::hash::Hash> crate::hash::Hash for Range<Idx> {
    #[inline]
    fn hash<__H: crate::hash::Hasher>(&self, state: &mut __H) {
        crate::hash::Hash::hash(&self.start, state);
        crate::hash::Hash::hash(&self.end, state)
    }
}Hash)]
71#[derive_const(#[automatically_derived]#[rustc_const_unstable(feature = "derive_const", issue = "143874")]#[stable(feature
= "new_range_api", since = "1.96.0")]
const impl<Idx: [const] crate::clone::Clone> crate::clone::Clone for
    Range<Idx> {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self {
        Range {
            start: crate::clone::Clone::clone(&self.start),
            end: crate::clone::Clone::clone(&self.end),
        }
    }
}Clone, #[automatically_derived]#[rustc_const_unstable(feature = "derive_const", issue = "143874")]#[stable(feature
= "new_range_api", since = "1.96.0")]
const impl<Idx: [const] crate::default::Default> crate::default::Default for
    Range<Idx> {
    #[inline]
    fn default() -> Self {
        Range {
            start: crate::default::Default::default(),
            end: crate::default::Default::default(),
        }
    }
}Default, #[automatically_derived]#[stable(feature = "new_range_api", since = "1.96.0")]
impl<Idx: crate::cmp::PartialEq> crate::marker::StructuralPartialEq for
    Range<Idx> {
}
#[automatically_derived]#[rustc_const_unstable(feature = "derive_const", issue = "143874")]#[stable(feature
= "new_range_api", since = "1.96.0")]
const impl<Idx: [const] crate::cmp::PartialEq> crate::cmp::PartialEq for
    Range<Idx> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.start == other.start && self.end == other.end
    }
}PartialEq, #[automatically_derived]#[rustc_const_unstable(feature = "derive_const", issue = "143874")]#[stable(feature
= "new_range_api", since = "1.96.0")]
const impl<Idx: [const] crate::cmp::Eq> crate::cmp::Eq for Range<Idx> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: crate::cmp::AssertParamIsEq<Idx>;
    }
}Eq)]
72#[stable(feature = "new_range_api", since = "1.96.0")]
73pub struct Range<Idx> {
74    /// The lower bound of the range (inclusive).
75    #[stable(feature = "new_range_api", since = "1.96.0")]
76    pub start: Idx,
77    /// The upper bound of the range (exclusive).
78    #[stable(feature = "new_range_api", since = "1.96.0")]
79    pub end: Idx,
80}
81
82#[stable(feature = "new_range_api", since = "1.96.0")]
83impl<Idx: fmt::Debug> fmt::Debug for Range<Idx> {
84    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
85        self.start.fmt(fmt)?;
86        fmt.write_fmt(format_args!(".."))write!(fmt, "..")?;
87        self.end.fmt(fmt)?;
88        Ok(())
89    }
90}
91
92impl<Idx: Step> Range<Idx> {
93    /// Creates an iterator over the elements within this range.
94    ///
95    /// Shorthand for `.clone().into_iter()`
96    ///
97    /// # Examples
98    ///
99    /// ```
100    /// use core::range::Range;
101    ///
102    /// let mut i = Range::from(3..9).iter().map(|n| n*n);
103    /// assert_eq!(i.next(), Some(9));
104    /// assert_eq!(i.next(), Some(16));
105    /// assert_eq!(i.next(), Some(25));
106    /// ```
107    #[stable(feature = "new_range_api", since = "1.96.0")]
108    #[inline]
109    pub fn iter(&self) -> RangeIter<Idx> {
110        self.clone().into_iter()
111    }
112}
113
114impl<Idx: PartialOrd<Idx>> Range<Idx> {
115    /// Returns `true` if `item` is contained in the range.
116    ///
117    /// # Examples
118    ///
119    /// ```
120    /// use core::range::Range;
121    ///
122    /// assert!(!Range::from(3..5).contains(&2));
123    /// assert!( Range::from(3..5).contains(&3));
124    /// assert!( Range::from(3..5).contains(&4));
125    /// assert!(!Range::from(3..5).contains(&5));
126    ///
127    /// assert!(!Range::from(3..3).contains(&3));
128    /// assert!(!Range::from(3..2).contains(&3));
129    ///
130    /// assert!( Range::from(0.0..1.0).contains(&0.5));
131    /// assert!(!Range::from(0.0..1.0).contains(&f32::NAN));
132    /// assert!(!Range::from(0.0..f32::NAN).contains(&0.5));
133    /// assert!(!Range::from(f32::NAN..1.0).contains(&0.5));
134    /// ```
135    #[inline]
136    #[stable(feature = "new_range_api", since = "1.96.0")]
137    #[rustc_const_unstable(feature = "const_range", issue = "none")]
138    pub const fn contains<U>(&self, item: &U) -> bool
139    where
140        Idx: [const] PartialOrd<U>,
141        U: ?Sized + [const] PartialOrd<Idx>,
142    {
143        <Self as RangeBounds<Idx>>::contains(self, item)
144    }
145
146    /// Returns `true` if the range contains no items.
147    ///
148    /// # Examples
149    ///
150    /// ```
151    /// use core::range::Range;
152    ///
153    /// assert!(!Range::from(3..5).is_empty());
154    /// assert!( Range::from(3..3).is_empty());
155    /// assert!( Range::from(3..2).is_empty());
156    /// ```
157    ///
158    /// The range is empty if either side is incomparable:
159    ///
160    /// ```
161    /// use core::range::Range;
162    ///
163    /// assert!(!Range::from(3.0..5.0).is_empty());
164    /// assert!( Range::from(3.0..f32::NAN).is_empty());
165    /// assert!( Range::from(f32::NAN..5.0).is_empty());
166    /// ```
167    #[inline]
168    #[stable(feature = "new_range_api", since = "1.96.0")]
169    #[rustc_const_unstable(feature = "const_range", issue = "none")]
170    #[expect(clippy::neg_cmp_op_on_partial_ord, reason = "incomparable ranges are empty")]
171    pub const fn is_empty(&self) -> bool
172    where
173        Idx: [const] PartialOrd,
174    {
175        !(self.start < self.end)
176    }
177}
178
179#[stable(feature = "new_range_api", since = "1.96.0")]
180#[rustc_const_unstable(feature = "const_range", issue = "none")]
181const impl<T> RangeBounds<T> for Range<T> {
182    fn start_bound(&self) -> Bound<&T> {
183        Included(&self.start)
184    }
185    fn end_bound(&self) -> Bound<&T> {
186        Excluded(&self.end)
187    }
188}
189
190// This impl intentionally does not have `T: ?Sized`;
191// see https://github.com/rust-lang/rust/pull/61584 for discussion of why.
192//
193/// If you need to use this implementation where `T` is unsized,
194/// consider using the `RangeBounds` impl for a 2-tuple of [`Bound<&T>`][Bound],
195/// i.e. replace `start..end` with `(Bound::Included(start), Bound::Excluded(end))`.
196#[stable(feature = "new_range_api", since = "1.96.0")]
197#[rustc_const_unstable(feature = "const_range", issue = "none")]
198const impl<T> RangeBounds<T> for Range<&T> {
199    fn start_bound(&self) -> Bound<&T> {
200        Included(self.start)
201    }
202    fn end_bound(&self) -> Bound<&T> {
203        Excluded(self.end)
204    }
205}
206
207#[unstable(feature = "range_into_bounds", issue = "136903")]
208#[rustc_const_unstable(feature = "const_range", issue = "none")]
209const impl<T> IntoBounds<T> for Range<T> {
210    fn into_bounds(self) -> (Bound<T>, Bound<T>) {
211        (Included(self.start), Excluded(self.end))
212    }
213}
214
215#[stable(feature = "new_range_api", since = "1.96.0")]
216#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
217const impl<T> From<Range<T>> for legacy::Range<T> {
218    #[inline]
219    fn from(value: Range<T>) -> Self {
220        Self { start: value.start, end: value.end }
221    }
222}
223#[stable(feature = "new_range_api", since = "1.96.0")]
224#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
225const impl<T> From<legacy::Range<T>> for Range<T> {
226    #[inline]
227    fn from(value: legacy::Range<T>) -> Self {
228        Self { start: value.start, end: value.end }
229    }
230}
231
232/// A range bounded inclusively below and above.
233///
234/// The `RangeInclusive` contains all values with `x >= start`
235/// and `x <= last`. It is empty unless `start <= last`.
236///
237/// # Examples
238///
239/// ```
240/// use core::range::RangeInclusive;
241///
242/// assert_eq!(RangeInclusive::from(3..=5), RangeInclusive { start: 3, last: 5 });
243/// assert_eq!(3 + 4 + 5, RangeInclusive::from(3..=5).into_iter().sum());
244/// ```
245///
246/// # Edition notes
247///
248/// It is planned that the syntax  `start..=last` will construct this
249/// type in a future edition, but it does not do so today.
250#[lang = "RangeInclusiveCopy"]
251#[derive(#[automatically_derived]#[stable(feature = "new_range_inclusive_api", since =
"1.95.0")]
impl<Idx: crate::clone::Clone> crate::clone::Clone for RangeInclusive<Idx> {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self {
        RangeInclusive {
            start: crate::clone::Clone::clone(&self.start),
            last: crate::clone::Clone::clone(&self.last),
        }
    }
}Clone, #[automatically_derived]#[stable(feature = "new_range_inclusive_api", since =
"1.95.0")]
impl<Idx: crate::marker::Copy> crate::marker::Copy for RangeInclusive<Idx> { }Copy, #[automatically_derived]#[stable(feature = "new_range_inclusive_api", since =
"1.95.0")]
impl<Idx: crate::cmp::PartialEq> crate::marker::StructuralPartialEq for
    RangeInclusive<Idx> {
}
#[automatically_derived]#[stable(feature = "new_range_inclusive_api", since =
"1.95.0")]
impl<Idx: crate::cmp::PartialEq> crate::cmp::PartialEq for RangeInclusive<Idx>
    {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.start == other.start && self.last == other.last
    }
}PartialEq, #[automatically_derived]#[stable(feature = "new_range_inclusive_api", since =
"1.95.0")]
impl<Idx: crate::cmp::Eq> crate::cmp::Eq for RangeInclusive<Idx> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: crate::cmp::AssertParamIsEq<Idx>;
    }
}Eq, #[automatically_derived]#[stable(feature = "new_range_inclusive_api", since =
"1.95.0")]
impl<Idx: crate::hash::Hash> crate::hash::Hash for RangeInclusive<Idx> {
    #[inline]
    fn hash<__H: crate::hash::Hasher>(&self, state: &mut __H) {
        crate::hash::Hash::hash(&self.start, state);
        crate::hash::Hash::hash(&self.last, state)
    }
}Hash)]
252#[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
253pub struct RangeInclusive<Idx> {
254    /// The lower bound of the range (inclusive).
255    #[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
256    pub start: Idx,
257    /// The upper bound of the range (inclusive).
258    #[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
259    pub last: Idx,
260}
261
262#[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
263impl<Idx: fmt::Debug> fmt::Debug for RangeInclusive<Idx> {
264    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
265        self.start.fmt(fmt)?;
266        fmt.write_fmt(format_args!("..="))write!(fmt, "..=")?;
267        self.last.fmt(fmt)?;
268        Ok(())
269    }
270}
271
272impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
273    /// Returns `true` if `item` is contained in the range.
274    ///
275    /// # Examples
276    ///
277    /// ```
278    /// use core::range::RangeInclusive;
279    ///
280    /// assert!(!RangeInclusive::from(3..=5).contains(&2));
281    /// assert!( RangeInclusive::from(3..=5).contains(&3));
282    /// assert!( RangeInclusive::from(3..=5).contains(&4));
283    /// assert!( RangeInclusive::from(3..=5).contains(&5));
284    /// assert!(!RangeInclusive::from(3..=5).contains(&6));
285    ///
286    /// assert!( RangeInclusive::from(3..=3).contains(&3));
287    /// assert!(!RangeInclusive::from(3..=2).contains(&3));
288    ///
289    /// assert!( RangeInclusive::from(0.0..=1.0).contains(&1.0));
290    /// assert!(!RangeInclusive::from(0.0..=1.0).contains(&f32::NAN));
291    /// assert!(!RangeInclusive::from(0.0..=f32::NAN).contains(&0.0));
292    /// assert!(!RangeInclusive::from(f32::NAN..=1.0).contains(&1.0));
293    /// ```
294    #[inline]
295    #[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
296    #[rustc_const_unstable(feature = "const_range", issue = "none")]
297    pub const fn contains<U>(&self, item: &U) -> bool
298    where
299        Idx: [const] PartialOrd<U>,
300        U: ?Sized + [const] PartialOrd<Idx>,
301    {
302        <Self as RangeBounds<Idx>>::contains(self, item)
303    }
304
305    /// Returns `true` if the range contains no items.
306    ///
307    /// # Examples
308    ///
309    /// ```
310    /// use core::range::RangeInclusive;
311    ///
312    /// assert!(!RangeInclusive::from(3..=5).is_empty());
313    /// assert!(!RangeInclusive::from(3..=3).is_empty());
314    /// assert!( RangeInclusive::from(3..=2).is_empty());
315    /// ```
316    ///
317    /// The range is empty if either side is incomparable:
318    ///
319    /// ```
320    /// use core::range::RangeInclusive;
321    ///
322    /// assert!(!RangeInclusive::from(3.0..=5.0).is_empty());
323    /// assert!( RangeInclusive::from(3.0..=f32::NAN).is_empty());
324    /// assert!( RangeInclusive::from(f32::NAN..=5.0).is_empty());
325    /// ```
326    #[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
327    #[inline]
328    #[rustc_const_unstable(feature = "const_range", issue = "none")]
329    #[expect(clippy::neg_cmp_op_on_partial_ord, reason = "incomparable ranges are empty")]
330    pub const fn is_empty(&self) -> bool
331    where
332        Idx: [const] PartialOrd,
333    {
334        !(self.start <= self.last)
335    }
336}
337
338impl<Idx: Step> RangeInclusive<Idx> {
339    /// Creates an iterator over the elements within this range.
340    ///
341    /// Shorthand for `.clone().into_iter()`
342    ///
343    /// # Examples
344    ///
345    /// ```
346    /// use core::range::RangeInclusive;
347    ///
348    /// let mut i = RangeInclusive::from(3..=8).iter().map(|n| n*n);
349    /// assert_eq!(i.next(), Some(9));
350    /// assert_eq!(i.next(), Some(16));
351    /// assert_eq!(i.next(), Some(25));
352    /// ```
353    #[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
354    #[inline]
355    pub fn iter(&self) -> RangeInclusiveIter<Idx> {
356        self.clone().into_iter()
357    }
358}
359
360#[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
361#[rustc_const_unstable(feature = "const_range", issue = "none")]
362const impl<T> RangeBounds<T> for RangeInclusive<T> {
363    fn start_bound(&self) -> Bound<&T> {
364        Included(&self.start)
365    }
366    fn end_bound(&self) -> Bound<&T> {
367        Included(&self.last)
368    }
369}
370
371// This impl intentionally does not have `T: ?Sized`;
372// see https://github.com/rust-lang/rust/pull/61584 for discussion of why.
373//
374/// If you need to use this implementation where `T` is unsized,
375/// consider using the `RangeBounds` impl for a 2-tuple of [`Bound<&T>`][Bound],
376/// i.e. replace `start..=end` with `(Bound::Included(start), Bound::Included(end))`.
377#[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
378#[rustc_const_unstable(feature = "const_range", issue = "none")]
379const impl<T> RangeBounds<T> for RangeInclusive<&T> {
380    fn start_bound(&self) -> Bound<&T> {
381        Included(self.start)
382    }
383    fn end_bound(&self) -> Bound<&T> {
384        Included(self.last)
385    }
386}
387
388// #[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
389#[unstable(feature = "range_into_bounds", issue = "136903")]
390#[rustc_const_unstable(feature = "const_range", issue = "none")]
391const impl<T> IntoBounds<T> for RangeInclusive<T> {
392    fn into_bounds(self) -> (Bound<T>, Bound<T>) {
393        (Included(self.start), Included(self.last))
394    }
395}
396
397#[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
398#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
399const impl<T> From<RangeInclusive<T>> for legacy::RangeInclusive<T> {
400    #[inline]
401    fn from(value: RangeInclusive<T>) -> Self {
402        Self::new(value.start, value.last)
403    }
404}
405#[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
406#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
407const impl<T> From<legacy::RangeInclusive<T>> for RangeInclusive<T> {
408    /// Converts from a legacy range to a non-legacy range, potentially panicking.
409    ///
410    /// # Panics
411    ///
412    /// If the legacy range iterator has been exhausted,
413    /// this function will either panic or return an empty range.
414    ///
415    /// # Examples
416    ///
417    /// ```
418    /// use core::range::legacy;
419    /// use core::range::RangeInclusive;
420    ///
421    /// let single: legacy::RangeInclusive<i32> = 0..=1;
422    /// let single = RangeInclusive::from(single);
423    /// assert_eq!((single.start, single.last), (0, 1));
424    ///
425    /// let empty: legacy::RangeInclusive<i32> = 0..=0;
426    /// let empty = RangeInclusive::from(empty);
427    /// assert_eq!((empty.start, empty.last), (0, 0));
428    /// ```
429    ///
430    /// ```
431    /// # // This test requires unwinding to work.
432    /// # // Disable it when unwinding isn't available.
433    /// # #[cfg(panic = "unwind")]
434    /// # fn main() {
435    /// use core::range::legacy;
436    /// use core::range::RangeInclusive;
437    /// use std::panic::catch_unwind;
438    ///
439    /// let mut exhausted: legacy::RangeInclusive<i32> = 0..=0;
440    /// exhausted.next();
441    /// let result = catch_unwind(|| RangeInclusive::from(exhausted));
442    /// // The `from` call either panicked or returned an empty range.
443    /// assert!(result.is_err() || result.is_ok_and(|range| range.is_empty()));
444    /// # }
445    /// # #[cfg(not(panic = "unwind"))]
446    /// # fn main() {}
447    /// ```
448    #[inline]
449    fn from(value: legacy::RangeInclusive<T>) -> Self {
450        if !!value.exhausted {
    {
        crate::panicking::panic_fmt(format_args!("attempted to convert from an exhausted `legacy::RangeInclusive`"));
    }
};assert!(
451            !value.exhausted,
452            "attempted to convert from an exhausted `legacy::RangeInclusive`"
453        );
454
455        let (start, last) = value.into_inner();
456        RangeInclusive { start, last }
457    }
458}
459
460/// A range only bounded inclusively below.
461///
462/// The `RangeFrom` contains all values with `x >= start`.
463///
464/// *Note*: Overflow in the [`IntoIterator`] implementation (when the contained
465/// data type reaches its numerical limit) is allowed to panic, wrap, or
466/// saturate. This behavior is defined by the implementation of the [`Step`]
467/// trait. For primitive integers, this follows the normal rules, and respects
468/// the overflow checks profile (panic in debug, wrap in release). Unlike
469/// its legacy counterpart, the iterator will only panic after yielding the
470/// maximum value when overflow checks are enabled.
471///
472/// [`Step`]: crate::iter::Step
473///
474/// # Examples
475///
476/// ```
477/// use core::range::RangeFrom;
478///
479/// assert_eq!(RangeFrom::from(2..), core::range::RangeFrom { start: 2 });
480/// assert_eq!(2 + 3 + 4, RangeFrom::from(2..).into_iter().take(3).sum());
481/// ```
482///
483/// # Edition notes
484///
485/// It is planned that the syntax  `start..` will construct this
486/// type in a future edition, but it does not do so today.
487#[lang = "RangeFromCopy"]
488#[derive(#[automatically_derived]#[stable(feature = "new_range_from_api", since =
"1.96.0")]
impl<Idx: crate::marker::Copy> crate::marker::Copy for RangeFrom<Idx> { }Copy, #[automatically_derived]#[stable(feature = "new_range_from_api", since =
"1.96.0")]
impl<Idx: crate::hash::Hash> crate::hash::Hash for RangeFrom<Idx> {
    #[inline]
    fn hash<__H: crate::hash::Hasher>(&self, state: &mut __H) {
        crate::hash::Hash::hash(&self.start, state)
    }
}Hash)]
489#[derive_const(#[automatically_derived]#[rustc_const_unstable(feature = "derive_const", issue = "143874")]#[stable(feature
= "new_range_from_api", since = "1.96.0")]
const impl<Idx: [const] crate::clone::Clone> crate::clone::Clone for
    RangeFrom<Idx> {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self {
        RangeFrom { start: crate::clone::Clone::clone(&self.start) }
    }
}Clone, #[automatically_derived]#[stable(feature = "new_range_from_api", since =
"1.96.0")]
impl<Idx: crate::cmp::PartialEq> crate::marker::StructuralPartialEq for
    RangeFrom<Idx> {
}
#[automatically_derived]#[rustc_const_unstable(feature = "derive_const", issue = "143874")]#[stable(feature
= "new_range_from_api", since = "1.96.0")]
const impl<Idx: [const] crate::cmp::PartialEq> crate::cmp::PartialEq for
    RangeFrom<Idx> {
    #[inline]
    fn eq(&self, other: &Self) -> bool { self.start == other.start }
}PartialEq, #[automatically_derived]#[rustc_const_unstable(feature = "derive_const", issue = "143874")]#[stable(feature
= "new_range_from_api", since = "1.96.0")]
const impl<Idx: [const] crate::cmp::Eq> crate::cmp::Eq for RangeFrom<Idx> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: crate::cmp::AssertParamIsEq<Idx>;
    }
}Eq)]
490#[stable(feature = "new_range_from_api", since = "1.96.0")]
491pub struct RangeFrom<Idx> {
492    /// The lower bound of the range (inclusive).
493    #[stable(feature = "new_range_from_api", since = "1.96.0")]
494    pub start: Idx,
495}
496
497#[stable(feature = "new_range_from_api", since = "1.96.0")]
498impl<Idx: fmt::Debug> fmt::Debug for RangeFrom<Idx> {
499    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
500        self.start.fmt(fmt)?;
501        fmt.write_fmt(format_args!(".."))write!(fmt, "..")?;
502        Ok(())
503    }
504}
505
506impl<Idx: Step> RangeFrom<Idx> {
507    /// Creates an iterator over the elements within this range.
508    ///
509    /// Shorthand for `.clone().into_iter()`
510    ///
511    /// # Examples
512    ///
513    /// ```
514    /// use core::range::RangeFrom;
515    ///
516    /// let mut i = RangeFrom::from(3..).iter().map(|n| n*n);
517    /// assert_eq!(i.next(), Some(9));
518    /// assert_eq!(i.next(), Some(16));
519    /// assert_eq!(i.next(), Some(25));
520    /// ```
521    #[stable(feature = "new_range_from_api", since = "1.96.0")]
522    #[inline]
523    pub fn iter(&self) -> RangeFromIter<Idx> {
524        self.clone().into_iter()
525    }
526}
527
528impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
529    /// Returns `true` if `item` is contained in the range.
530    ///
531    /// # Examples
532    ///
533    /// ```
534    /// use core::range::RangeFrom;
535    ///
536    /// assert!(!RangeFrom::from(3..).contains(&2));
537    /// assert!( RangeFrom::from(3..).contains(&3));
538    /// assert!( RangeFrom::from(3..).contains(&1_000_000_000));
539    ///
540    /// assert!( RangeFrom::from(0.0..).contains(&0.5));
541    /// assert!(!RangeFrom::from(0.0..).contains(&f32::NAN));
542    /// assert!(!RangeFrom::from(f32::NAN..).contains(&0.5));
543    /// ```
544    #[inline]
545    #[stable(feature = "new_range_from_api", since = "1.96.0")]
546    #[rustc_const_unstable(feature = "const_range", issue = "none")]
547    pub const fn contains<U>(&self, item: &U) -> bool
548    where
549        Idx: [const] PartialOrd<U>,
550        U: ?Sized + [const] PartialOrd<Idx>,
551    {
552        <Self as RangeBounds<Idx>>::contains(self, item)
553    }
554}
555
556#[stable(feature = "new_range_from_api", since = "1.96.0")]
557#[rustc_const_unstable(feature = "const_range", issue = "none")]
558const impl<T> RangeBounds<T> for RangeFrom<T> {
559    fn start_bound(&self) -> Bound<&T> {
560        Included(&self.start)
561    }
562    fn end_bound(&self) -> Bound<&T> {
563        Unbounded
564    }
565}
566
567// This impl intentionally does not have `T: ?Sized`;
568// see https://github.com/rust-lang/rust/pull/61584 for discussion of why.
569//
570/// If you need to use this implementation where `T` is unsized,
571/// consider using the `RangeBounds` impl for a 2-tuple of [`Bound<&T>`][Bound],
572/// i.e. replace `start..` with `(Bound::Included(start), Bound::Unbounded)`.
573#[stable(feature = "new_range_from_api", since = "1.96.0")]
574#[rustc_const_unstable(feature = "const_range", issue = "none")]
575const impl<T> RangeBounds<T> for RangeFrom<&T> {
576    fn start_bound(&self) -> Bound<&T> {
577        Included(self.start)
578    }
579    fn end_bound(&self) -> Bound<&T> {
580        Unbounded
581    }
582}
583
584#[unstable(feature = "range_into_bounds", issue = "136903")]
585#[rustc_const_unstable(feature = "const_range", issue = "none")]
586const impl<T> IntoBounds<T> for RangeFrom<T> {
587    fn into_bounds(self) -> (Bound<T>, Bound<T>) {
588        (Included(self.start), Unbounded)
589    }
590}
591
592#[unstable(feature = "one_sided_range", issue = "69780")]
593#[rustc_const_unstable(feature = "const_range", issue = "none")]
594const impl<T> OneSidedRange<T> for RangeFrom<T>
595where
596    Self: RangeBounds<T>,
597{
598    fn bound(self) -> (OneSidedRangeBound, T) {
599        (OneSidedRangeBound::StartInclusive, self.start)
600    }
601}
602
603#[stable(feature = "new_range_from_api", since = "1.96.0")]
604#[rustc_const_unstable(feature = "const_index", issue = "143775")]
605const impl<T> From<RangeFrom<T>> for legacy::RangeFrom<T> {
606    #[inline]
607    fn from(value: RangeFrom<T>) -> Self {
608        Self { start: value.start }
609    }
610}
611#[stable(feature = "new_range_from_api", since = "1.96.0")]
612#[rustc_const_unstable(feature = "const_index", issue = "143775")]
613const impl<T> From<legacy::RangeFrom<T>> for RangeFrom<T> {
614    #[inline]
615    fn from(value: legacy::RangeFrom<T>) -> Self {
616        Self { start: value.start }
617    }
618}
619
620/// A range only bounded inclusively above.
621///
622/// The `RangeToInclusive` contains all values with `x <= last`.
623/// It cannot serve as an [`Iterator`] because it doesn't have a starting point.
624///
625/// # Examples
626///
627/// ```standalone_crate
628/// #![feature(new_range)]
629/// assert_eq!((..=5), std::range::RangeToInclusive { last: 5 });
630/// ```
631///
632/// It does not have an [`IntoIterator`] implementation, so you can't use it in a
633/// `for` loop directly. This won't compile:
634///
635/// ```compile_fail,E0277
636/// // error[E0277]: the trait bound `std::range::RangeToInclusive<{integer}>:
637/// // std::iter::Iterator` is not satisfied
638/// for i in ..=5 {
639///     // ...
640/// }
641/// ```
642///
643/// When used as a [slicing index], `RangeToInclusive` produces a slice of all
644/// array elements up to and including the index indicated by `last`.
645///
646/// ```
647/// let arr = [0, 1, 2, 3, 4];
648/// assert_eq!(arr[ ..  ], [0, 1, 2, 3, 4]);
649/// assert_eq!(arr[ .. 3], [0, 1, 2      ]);
650/// assert_eq!(arr[ ..=3], [0, 1, 2, 3   ]); // This is a `RangeToInclusive`
651/// assert_eq!(arr[1..  ], [   1, 2, 3, 4]);
652/// assert_eq!(arr[1.. 3], [   1, 2      ]);
653/// assert_eq!(arr[1..=3], [   1, 2, 3   ]);
654/// ```
655///
656/// [slicing index]: crate::slice::SliceIndex
657///
658/// # Edition notes
659///
660/// It is planned that the syntax  `..=last` will construct this
661/// type in a future edition, but it does not do so today.
662#[lang = "RangeToInclusiveCopy"]
663#[doc(alias = "..=")]
664#[derive(#[automatically_derived]#[stable(feature = "new_range_to_inclusive_api", since
= "1.96.0")]
impl<Idx: crate::marker::Copy> crate::marker::Copy for RangeToInclusive<Idx> {
}Copy, #[automatically_derived]#[stable(feature = "new_range_to_inclusive_api", since
= "1.96.0")]
impl<Idx: crate::clone::Clone> crate::clone::Clone for RangeToInclusive<Idx> {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self {
        RangeToInclusive { last: crate::clone::Clone::clone(&self.last) }
    }
}Clone, #[automatically_derived]#[stable(feature = "new_range_to_inclusive_api", since
= "1.96.0")]
impl<Idx: crate::cmp::PartialEq> crate::marker::StructuralPartialEq for
    RangeToInclusive<Idx> {
}
#[automatically_derived]#[stable(feature = "new_range_to_inclusive_api", since
= "1.96.0")]
impl<Idx: crate::cmp::PartialEq> crate::cmp::PartialEq for
    RangeToInclusive<Idx> {
    #[inline]
    fn eq(&self, other: &Self) -> bool { self.last == other.last }
}PartialEq, #[automatically_derived]#[stable(feature = "new_range_to_inclusive_api", since
= "1.96.0")]
impl<Idx: crate::cmp::Eq> crate::cmp::Eq for RangeToInclusive<Idx> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: crate::cmp::AssertParamIsEq<Idx>;
    }
}Eq, #[automatically_derived]#[stable(feature = "new_range_to_inclusive_api", since
= "1.96.0")]
impl<Idx: crate::hash::Hash> crate::hash::Hash for RangeToInclusive<Idx> {
    #[inline]
    fn hash<__H: crate::hash::Hasher>(&self, state: &mut __H) {
        crate::hash::Hash::hash(&self.last, state)
    }
}Hash)]
665#[stable(feature = "new_range_to_inclusive_api", since = "1.96.0")]
666pub struct RangeToInclusive<Idx> {
667    /// The upper bound of the range (inclusive)
668    #[stable(feature = "new_range_to_inclusive_api", since = "1.96.0")]
669    pub last: Idx,
670}
671
672#[stable(feature = "new_range_to_inclusive_api", since = "1.96.0")]
673impl<Idx: fmt::Debug> fmt::Debug for RangeToInclusive<Idx> {
674    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
675        fmt.write_fmt(format_args!("..="))write!(fmt, "..=")?;
676        self.last.fmt(fmt)?;
677        Ok(())
678    }
679}
680
681impl<Idx: PartialOrd<Idx>> RangeToInclusive<Idx> {
682    /// Returns `true` if `item` is contained in the range.
683    ///
684    /// # Examples
685    ///
686    /// ```
687    /// assert!( (..=5).contains(&-1_000_000_000));
688    /// assert!( (..=5).contains(&5));
689    /// assert!(!(..=5).contains(&6));
690    ///
691    /// assert!( (..=1.0).contains(&1.0));
692    /// assert!(!(..=1.0).contains(&f32::NAN));
693    /// assert!(!(..=f32::NAN).contains(&0.5));
694    /// ```
695    #[inline]
696    #[stable(feature = "new_range_to_inclusive_api", since = "1.96.0")]
697    #[rustc_const_unstable(feature = "const_range", issue = "none")]
698    pub const fn contains<U>(&self, item: &U) -> bool
699    where
700        Idx: [const] PartialOrd<U>,
701        U: ?Sized + [const] PartialOrd<Idx>,
702    {
703        <Self as RangeBounds<Idx>>::contains(self, item)
704    }
705}
706
707#[stable(feature = "new_range_to_inclusive_api", since = "1.96.0")]
708impl<T> From<legacy::RangeToInclusive<T>> for RangeToInclusive<T> {
709    fn from(value: legacy::RangeToInclusive<T>) -> Self {
710        Self { last: value.end }
711    }
712}
713#[stable(feature = "new_range_to_inclusive_api", since = "1.96.0")]
714impl<T> From<RangeToInclusive<T>> for legacy::RangeToInclusive<T> {
715    fn from(value: RangeToInclusive<T>) -> Self {
716        Self { end: value.last }
717    }
718}
719
720// RangeToInclusive<Idx> cannot impl From<RangeTo<Idx>>
721// because underflow would be possible with (..0).into()
722
723#[stable(feature = "new_range_to_inclusive_api", since = "1.96.0")]
724#[rustc_const_unstable(feature = "const_range", issue = "none")]
725const impl<T> RangeBounds<T> for RangeToInclusive<T> {
726    fn start_bound(&self) -> Bound<&T> {
727        Unbounded
728    }
729    fn end_bound(&self) -> Bound<&T> {
730        Included(&self.last)
731    }
732}
733
734#[stable(feature = "new_range_to_inclusive_api", since = "1.96.0")]
735#[rustc_const_unstable(feature = "const_range", issue = "none")]
736const impl<T> RangeBounds<T> for RangeToInclusive<&T> {
737    fn start_bound(&self) -> Bound<&T> {
738        Unbounded
739    }
740    fn end_bound(&self) -> Bound<&T> {
741        Included(self.last)
742    }
743}
744
745#[unstable(feature = "range_into_bounds", issue = "136903")]
746#[rustc_const_unstable(feature = "const_range", issue = "none")]
747const impl<T> IntoBounds<T> for RangeToInclusive<T> {
748    fn into_bounds(self) -> (Bound<T>, Bound<T>) {
749        (Unbounded, Included(self.last))
750    }
751}
752
753#[unstable(feature = "one_sided_range", issue = "69780")]
754#[rustc_const_unstable(feature = "const_range", issue = "none")]
755const impl<T> OneSidedRange<T> for RangeToInclusive<T>
756where
757    Self: RangeBounds<T>,
758{
759    fn bound(self) -> (OneSidedRangeBound, T) {
760        (OneSidedRangeBound::EndInclusive, self.last)
761    }
762}
763
764/// Computes the intersection of two ranges, returning the most specific range type possible.
765///
766/// This trait is not implemented for all combinations, only the ones with a good
767/// `Self::Intersection` type available.
768///
769/// However, because `(Bound<T>, Bound<T>): RangeIntersect`, you can always use
770/// `a.into_bounds().intersect(b.into_bounds())` for any two [`IntoBounds`] types if needed.
771///
772/// # Examples
773///
774/// ```
775/// #![feature(range_intersect)]
776/// use core::range::{Range, RangeFrom, RangeInclusive, RangeIntersect};
777///
778/// assert_eq!(
779///     Range::from(-12..387).intersect(Range::from(0..256)),
780///     Range::from(0..256),
781/// );
782/// assert_eq!(
783///     RangeFrom::from(3..).intersect(..5),
784///     Range::from(3..5),
785/// );
786/// assert_eq!(
787///     RangeInclusive::from(1..=9).intersect(RangeFrom::from(5..)),
788///     RangeInclusive::from(5..=9),
789/// );
790/// ```
791///
792/// Combine with `is_empty` to determine if two ranges overlap:
793///
794/// ```
795/// #![feature(range_intersect)]
796/// use core::range::{Range, RangeFrom, RangeIntersect};
797///
798/// assert!(!RangeFrom::from(3..).intersect(..5).is_empty());
799/// assert!(!Range::from(-12..387).intersect(Range::from(0..256)).is_empty());
800/// assert!(Range::from(1..5).intersect(RangeFrom::from(6..)).is_empty());
801/// ```
802#[unstable(feature = "range_intersect", issue = "165428")]
803#[rustc_const_unstable(feature = "const_range", issue = "none")]
804pub const trait RangeIntersect<Rhs = Self>: Sized {
805    /// The resulting range type of the intersection.
806    #[unstable(feature = "range_intersect", issue = "165428")]
807    type Intersection;
808
809    /// Computes the intersection of `self` and `rhs`.
810    #[unstable(feature = "range_intersect", issue = "165428")]
811    #[must_use]
812    fn intersect(self, rhs: Rhs) -> Self::Intersection;
813}
814
815#[unstable(feature = "range_intersect", issue = "165428")]
816#[rustc_const_unstable(feature = "const_range", issue = "none")]
817const impl<T> RangeIntersect for (Bound<T>, Bound<T>)
818where
819    T: [const] Ord + [const] Destruct,
820{
821    type Intersection = Self;
822
823    #[inline]
824    fn intersect(self, rhs: Self) -> Self::Intersection {
825        let (self_start, self_end) = self;
826        let (other_start, other_end) = rhs;
827
828        let start = match (self_start, other_start) {
829            (Included(a), Included(b)) => Included(Ord::max(a, b)),
830            (Excluded(a), Excluded(b)) => Excluded(Ord::max(a, b)),
831            (Unbounded, Unbounded) => Unbounded,
832
833            (x, Unbounded) | (Unbounded, x) => x,
834
835            (Included(i), Excluded(e)) | (Excluded(e), Included(i)) => {
836                if i > e {
837                    Included(i)
838                } else {
839                    Excluded(e)
840                }
841            }
842        };
843        let end = match (self_end, other_end) {
844            (Included(a), Included(b)) => Included(Ord::min(a, b)),
845            (Excluded(a), Excluded(b)) => Excluded(Ord::min(a, b)),
846            (Unbounded, Unbounded) => Unbounded,
847
848            (x, Unbounded) | (Unbounded, x) => x,
849
850            (Included(i), Excluded(e)) | (Excluded(e), Included(i)) => {
851                if i < e {
852                    Included(i)
853                } else {
854                    Excluded(e)
855                }
856            }
857        };
858
859        (start, end)
860    }
861}
862
863#[unstable(feature = "range_intersect", issue = "165428")]
864#[rustc_const_unstable(feature = "const_range", issue = "none")]
865const impl<T> RangeIntersect for Range<T>
866where
867    T: [const] Ord + [const] Destruct,
868{
869    type Intersection = Range<T>;
870
871    #[inline]
872    fn intersect(self, rhs: Self) -> Self::Intersection {
873        Range { start: Ord::max(self.start, rhs.start), end: Ord::min(self.end, rhs.end) }
874    }
875}
876
877#[unstable(feature = "range_intersect", issue = "165428")]
878#[rustc_const_unstable(feature = "const_range", issue = "none")]
879const impl<T> RangeIntersect<RangeFrom<T>> for Range<T>
880where
881    T: [const] Ord + [const] Destruct,
882{
883    type Intersection = Range<T>;
884
885    #[inline]
886    fn intersect(self, rhs: RangeFrom<T>) -> Self::Intersection {
887        Range { start: Ord::max(self.start, rhs.start), end: self.end }
888    }
889}
890
891#[unstable(feature = "range_intersect", issue = "165428")]
892#[rustc_const_unstable(feature = "const_range", issue = "none")]
893const impl<T> RangeIntersect<RangeTo<T>> for Range<T>
894where
895    T: [const] Ord + [const] Destruct,
896{
897    type Intersection = Range<T>;
898
899    #[inline]
900    fn intersect(self, rhs: RangeTo<T>) -> Self::Intersection {
901        Range { start: self.start, end: Ord::min(self.end, rhs.end) }
902    }
903}
904
905#[unstable(feature = "range_intersect", issue = "165428")]
906#[rustc_const_unstable(feature = "const_range", issue = "none")]
907const impl<T> RangeIntersect<RangeFull> for Range<T> {
908    type Intersection = Range<T>;
909
910    #[inline]
911    fn intersect(self, _rhs: RangeFull) -> Self::Intersection {
912        self
913    }
914}
915
916#[unstable(feature = "range_intersect", issue = "165428")]
917#[rustc_const_unstable(feature = "const_range", issue = "none")]
918const impl<T> RangeIntersect<Range<T>> for RangeFrom<T>
919where
920    T: [const] Ord + [const] Destruct,
921{
922    type Intersection = Range<T>;
923
924    #[inline]
925    fn intersect(self, rhs: Range<T>) -> Self::Intersection {
926        Range { start: Ord::max(self.start, rhs.start), end: rhs.end }
927    }
928}
929
930#[unstable(feature = "range_intersect", issue = "165428")]
931#[rustc_const_unstable(feature = "const_range", issue = "none")]
932const impl<T> RangeIntersect for RangeFrom<T>
933where
934    T: [const] Ord + [const] Destruct,
935{
936    type Intersection = RangeFrom<T>;
937
938    #[inline]
939    fn intersect(self, rhs: Self) -> Self::Intersection {
940        RangeFrom { start: Ord::max(self.start, rhs.start) }
941    }
942}
943
944#[unstable(feature = "range_intersect", issue = "165428")]
945#[rustc_const_unstable(feature = "const_range", issue = "none")]
946const impl<T> RangeIntersect<RangeTo<T>> for RangeFrom<T> {
947    type Intersection = Range<T>;
948
949    #[inline]
950    fn intersect(self, rhs: RangeTo<T>) -> Self::Intersection {
951        Range { start: self.start, end: rhs.end }
952    }
953}
954
955#[unstable(feature = "range_intersect", issue = "165428")]
956#[rustc_const_unstable(feature = "const_range", issue = "none")]
957const impl<T> RangeIntersect<RangeFull> for RangeFrom<T> {
958    type Intersection = RangeFrom<T>;
959
960    #[inline]
961    fn intersect(self, _rhs: RangeFull) -> Self::Intersection {
962        self
963    }
964}
965
966#[unstable(feature = "range_intersect", issue = "165428")]
967#[rustc_const_unstable(feature = "const_range", issue = "none")]
968const impl<T> RangeIntersect<RangeInclusive<T>> for RangeFrom<T>
969where
970    T: [const] Ord + [const] Destruct,
971{
972    type Intersection = RangeInclusive<T>;
973
974    #[inline]
975    fn intersect(self, rhs: RangeInclusive<T>) -> Self::Intersection {
976        RangeInclusive { start: Ord::max(self.start, rhs.start), last: rhs.last }
977    }
978}
979
980#[unstable(feature = "range_intersect", issue = "165428")]
981#[rustc_const_unstable(feature = "const_range", issue = "none")]
982const impl<T> RangeIntersect<RangeToInclusive<T>> for RangeFrom<T> {
983    type Intersection = RangeInclusive<T>;
984
985    #[inline]
986    fn intersect(self, rhs: RangeToInclusive<T>) -> Self::Intersection {
987        RangeInclusive { start: self.start, last: rhs.last }
988    }
989}
990
991#[unstable(feature = "range_intersect", issue = "165428")]
992#[rustc_const_unstable(feature = "const_range", issue = "none")]
993const impl<T> RangeIntersect<Range<T>> for RangeTo<T>
994where
995    T: [const] Ord + [const] Destruct,
996{
997    type Intersection = Range<T>;
998
999    #[inline]
1000    fn intersect(self, rhs: Range<T>) -> Self::Intersection {
1001        Range { start: rhs.start, end: Ord::min(self.end, rhs.end) }
1002    }
1003}
1004
1005#[unstable(feature = "range_intersect", issue = "165428")]
1006#[rustc_const_unstable(feature = "const_range", issue = "none")]
1007const impl<T> RangeIntersect<RangeFrom<T>> for RangeTo<T> {
1008    type Intersection = Range<T>;
1009
1010    #[inline]
1011    fn intersect(self, rhs: RangeFrom<T>) -> Self::Intersection {
1012        Range { start: rhs.start, end: self.end }
1013    }
1014}
1015
1016#[unstable(feature = "range_intersect", issue = "165428")]
1017#[rustc_const_unstable(feature = "const_range", issue = "none")]
1018const impl<T> RangeIntersect for RangeTo<T>
1019where
1020    T: [const] Ord + [const] Destruct,
1021{
1022    type Intersection = RangeTo<T>;
1023
1024    #[inline]
1025    fn intersect(self, rhs: Self) -> Self::Intersection {
1026        RangeTo { end: Ord::min(self.end, rhs.end) }
1027    }
1028}
1029
1030#[unstable(feature = "range_intersect", issue = "165428")]
1031#[rustc_const_unstable(feature = "const_range", issue = "none")]
1032const impl<T> RangeIntersect<RangeFull> for RangeTo<T> {
1033    type Intersection = RangeTo<T>;
1034
1035    #[inline]
1036    fn intersect(self, _rhs: RangeFull) -> Self::Intersection {
1037        self
1038    }
1039}
1040
1041#[unstable(feature = "range_intersect", issue = "165428")]
1042#[rustc_const_unstable(feature = "const_range", issue = "none")]
1043const impl<T> RangeIntersect<Range<T>> for RangeFull {
1044    type Intersection = Range<T>;
1045
1046    #[inline]
1047    fn intersect(self, rhs: Range<T>) -> Self::Intersection {
1048        rhs
1049    }
1050}
1051
1052#[unstable(feature = "range_intersect", issue = "165428")]
1053#[rustc_const_unstable(feature = "const_range", issue = "none")]
1054const impl<T> RangeIntersect<RangeFrom<T>> for RangeFull {
1055    type Intersection = RangeFrom<T>;
1056
1057    #[inline]
1058    fn intersect(self, rhs: RangeFrom<T>) -> Self::Intersection {
1059        rhs
1060    }
1061}
1062
1063#[unstable(feature = "range_intersect", issue = "165428")]
1064#[rustc_const_unstable(feature = "const_range", issue = "none")]
1065const impl<T> RangeIntersect<RangeTo<T>> for RangeFull {
1066    type Intersection = RangeTo<T>;
1067
1068    #[inline]
1069    fn intersect(self, rhs: RangeTo<T>) -> Self::Intersection {
1070        rhs
1071    }
1072}
1073
1074#[unstable(feature = "range_intersect", issue = "165428")]
1075#[rustc_const_unstable(feature = "const_range", issue = "none")]
1076const impl RangeIntersect for RangeFull {
1077    type Intersection = RangeFull;
1078
1079    #[inline]
1080    fn intersect(self, _rhs: RangeFull) -> Self::Intersection {
1081        RangeFull
1082    }
1083}
1084
1085#[unstable(feature = "range_intersect", issue = "165428")]
1086#[rustc_const_unstable(feature = "const_range", issue = "none")]
1087const impl<T> RangeIntersect<RangeInclusive<T>> for RangeFull {
1088    type Intersection = RangeInclusive<T>;
1089
1090    #[inline]
1091    fn intersect(self, rhs: RangeInclusive<T>) -> Self::Intersection {
1092        rhs
1093    }
1094}
1095
1096#[unstable(feature = "range_intersect", issue = "165428")]
1097#[rustc_const_unstable(feature = "const_range", issue = "none")]
1098const impl<T> RangeIntersect<RangeToInclusive<T>> for RangeFull {
1099    type Intersection = RangeToInclusive<T>;
1100
1101    #[inline]
1102    fn intersect(self, rhs: RangeToInclusive<T>) -> Self::Intersection {
1103        rhs
1104    }
1105}
1106
1107#[unstable(feature = "range_intersect", issue = "165428")]
1108#[rustc_const_unstable(feature = "const_range", issue = "none")]
1109const impl<T> RangeIntersect<RangeFrom<T>> for RangeInclusive<T>
1110where
1111    T: [const] Ord + [const] Destruct,
1112{
1113    type Intersection = RangeInclusive<T>;
1114
1115    #[inline]
1116    fn intersect(self, rhs: RangeFrom<T>) -> Self::Intersection {
1117        RangeInclusive { start: Ord::max(self.start, rhs.start), last: self.last }
1118    }
1119}
1120
1121#[unstable(feature = "range_intersect", issue = "165428")]
1122#[rustc_const_unstable(feature = "const_range", issue = "none")]
1123const impl<T> RangeIntersect<RangeFull> for RangeInclusive<T> {
1124    type Intersection = RangeInclusive<T>;
1125
1126    #[inline]
1127    fn intersect(self, _rhs: RangeFull) -> Self::Intersection {
1128        self
1129    }
1130}
1131
1132#[unstable(feature = "range_intersect", issue = "165428")]
1133#[rustc_const_unstable(feature = "const_range", issue = "none")]
1134const impl<T> RangeIntersect for RangeInclusive<T>
1135where
1136    T: [const] Ord + [const] Destruct,
1137{
1138    type Intersection = RangeInclusive<T>;
1139
1140    #[inline]
1141    fn intersect(self, rhs: Self) -> Self::Intersection {
1142        RangeInclusive {
1143            start: Ord::max(self.start, rhs.start),
1144            last: Ord::min(self.last, rhs.last),
1145        }
1146    }
1147}
1148
1149#[unstable(feature = "range_intersect", issue = "165428")]
1150#[rustc_const_unstable(feature = "const_range", issue = "none")]
1151const impl<T> RangeIntersect<RangeToInclusive<T>> for RangeInclusive<T>
1152where
1153    T: [const] Ord + [const] Destruct,
1154{
1155    type Intersection = RangeInclusive<T>;
1156
1157    #[inline]
1158    fn intersect(self, rhs: RangeToInclusive<T>) -> Self::Intersection {
1159        RangeInclusive { start: self.start, last: Ord::min(self.last, rhs.last) }
1160    }
1161}
1162
1163#[unstable(feature = "range_intersect", issue = "165428")]
1164#[rustc_const_unstable(feature = "const_range", issue = "none")]
1165const impl<T> RangeIntersect<RangeFrom<T>> for RangeToInclusive<T> {
1166    type Intersection = RangeInclusive<T>;
1167
1168    #[inline]
1169    fn intersect(self, rhs: RangeFrom<T>) -> Self::Intersection {
1170        RangeInclusive { start: rhs.start, last: self.last }
1171    }
1172}
1173
1174#[unstable(feature = "range_intersect", issue = "165428")]
1175#[rustc_const_unstable(feature = "const_range", issue = "none")]
1176const impl<T> RangeIntersect<RangeFull> for RangeToInclusive<T> {
1177    type Intersection = RangeToInclusive<T>;
1178
1179    #[inline]
1180    fn intersect(self, _rhs: RangeFull) -> Self::Intersection {
1181        self
1182    }
1183}
1184
1185#[unstable(feature = "range_intersect", issue = "165428")]
1186#[rustc_const_unstable(feature = "const_range", issue = "none")]
1187const impl<T> RangeIntersect<RangeInclusive<T>> for RangeToInclusive<T>
1188where
1189    T: [const] Ord + [const] Destruct,
1190{
1191    type Intersection = RangeInclusive<T>;
1192
1193    #[inline]
1194    fn intersect(self, rhs: RangeInclusive<T>) -> Self::Intersection {
1195        RangeInclusive { start: rhs.start, last: Ord::min(self.last, rhs.last) }
1196    }
1197}
1198
1199#[unstable(feature = "range_intersect", issue = "165428")]
1200#[rustc_const_unstable(feature = "const_range", issue = "none")]
1201const impl<T> RangeIntersect for RangeToInclusive<T>
1202where
1203    T: [const] Ord + [const] Destruct,
1204{
1205    type Intersection = RangeToInclusive<T>;
1206
1207    #[inline]
1208    fn intersect(self, rhs: Self) -> Self::Intersection {
1209        RangeToInclusive { last: Ord::min(self.last, rhs.last) }
1210    }
1211}