1use crate::fmt;
19use crate::hash::Hash;
20
21mod iter;
22
23#[stable(feature = "new_range_api_legacy", since = "1.98.0")]
24pub mod legacy;
25
26use core::ops::Bound::{self, Excluded, Included, Unbounded};
27
28#[doc(inline)]
29#[stable(feature = "new_range_from_api", since = "1.96.0")]
30pub use iter::RangeFromIter;
31#[doc(inline)]
32#[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
33pub use iter::RangeInclusiveIter;
34#[doc(inline)]
35#[stable(feature = "new_range_api", since = "1.96.0")]
36pub use iter::RangeIter;
37
38use crate::iter::Step;
39use crate::ops::{IntoBounds, OneSidedRange, OneSidedRangeBound, RangeBounds};
42#[doc(inline)]
43#[stable(feature = "new_range_api_exports", since = "1.98.0")]
44pub use crate::ops::{RangeFull, RangeTo};
45
46#[lang = "RangeCopy"]
65#[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)]
66#[derive_const(#[automatically_derived]
#[rustc_const_unstable(feature = "derive_const", issue = "118304")]
#[stable(feature = "new_range_api", since = "1.96.0")]
const impl<Idx: [const] crate::clone::Clone> crate::clone::Clone for
Range<Idx> {
#[inline]
fn clone(&self) -> Range<Idx> {
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 = "118304")]
#[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() -> Range<Idx> {
Range {
start: crate::default::Default::default(),
end: crate::default::Default::default(),
}
}
}Default, #[automatically_derived]
#[rustc_const_unstable(feature = "derive_const", issue = "118304")]
#[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: &Range<Idx>) -> bool {
self.start == other.start && self.end == other.end
}
}PartialEq, #[automatically_derived]
#[rustc_const_unstable(feature = "derive_const", issue = "118304")]
#[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)]
67#[stable(feature = "new_range_api", since = "1.96.0")]
68pub struct Range<Idx> {
69 #[stable(feature = "new_range_api", since = "1.96.0")]
71 pub start: Idx,
72 #[stable(feature = "new_range_api", since = "1.96.0")]
74 pub end: Idx,
75}
76
77#[stable(feature = "new_range_api", since = "1.96.0")]
78impl<Idx: fmt::Debug> fmt::Debug for Range<Idx> {
79 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
80 self.start.fmt(fmt)?;
81 fmt.write_fmt(format_args!(".."))write!(fmt, "..")?;
82 self.end.fmt(fmt)?;
83 Ok(())
84 }
85}
86
87impl<Idx: Step> Range<Idx> {
88 #[stable(feature = "new_range_api", since = "1.96.0")]
103 #[inline]
104 pub fn iter(&self) -> RangeIter<Idx> {
105 self.clone().into_iter()
106 }
107}
108
109impl<Idx: PartialOrd<Idx>> Range<Idx> {
110 #[inline]
131 #[stable(feature = "new_range_api", since = "1.96.0")]
132 #[rustc_const_unstable(feature = "const_range", issue = "none")]
133 pub const fn contains<U>(&self, item: &U) -> bool
134 where
135 Idx: [const] PartialOrd<U>,
136 U: ?Sized + [const] PartialOrd<Idx>,
137 {
138 <Self as RangeBounds<Idx>>::contains(self, item)
139 }
140
141 #[inline]
163 #[stable(feature = "new_range_api", since = "1.96.0")]
164 #[rustc_const_unstable(feature = "const_range", issue = "none")]
165 pub const fn is_empty(&self) -> bool
166 where
167 Idx: [const] PartialOrd,
168 {
169 !(self.start < self.end)
170 }
171}
172
173#[stable(feature = "new_range_api", since = "1.96.0")]
174#[rustc_const_unstable(feature = "const_range", issue = "none")]
175const impl<T> RangeBounds<T> for Range<T> {
176 fn start_bound(&self) -> Bound<&T> {
177 Included(&self.start)
178 }
179 fn end_bound(&self) -> Bound<&T> {
180 Excluded(&self.end)
181 }
182}
183
184#[stable(feature = "new_range_api", since = "1.96.0")]
191#[rustc_const_unstable(feature = "const_range", issue = "none")]
192const impl<T> RangeBounds<T> for Range<&T> {
193 fn start_bound(&self) -> Bound<&T> {
194 Included(self.start)
195 }
196 fn end_bound(&self) -> Bound<&T> {
197 Excluded(self.end)
198 }
199}
200
201#[unstable(feature = "range_into_bounds", issue = "136903")]
202#[rustc_const_unstable(feature = "const_range", issue = "none")]
203const impl<T> IntoBounds<T> for Range<T> {
204 fn into_bounds(self) -> (Bound<T>, Bound<T>) {
205 (Included(self.start), Excluded(self.end))
206 }
207}
208
209#[stable(feature = "new_range_api", since = "1.96.0")]
210#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
211const impl<T> From<Range<T>> for legacy::Range<T> {
212 #[inline]
213 fn from(value: Range<T>) -> Self {
214 Self { start: value.start, end: value.end }
215 }
216}
217#[stable(feature = "new_range_api", since = "1.96.0")]
218#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
219const impl<T> From<legacy::Range<T>> for Range<T> {
220 #[inline]
221 fn from(value: legacy::Range<T>) -> Self {
222 Self { start: value.start, end: value.end }
223 }
224}
225
226#[lang = "RangeInclusiveCopy"]
245#[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]
fn clone(&self) -> RangeInclusive<Idx> {
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::cmp::PartialEq for RangeInclusive<Idx>
{
#[inline]
fn eq(&self, other: &RangeInclusive<Idx>) -> 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)]
246#[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
247pub struct RangeInclusive<Idx> {
248 #[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
250 pub start: Idx,
251 #[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
253 pub last: Idx,
254}
255
256#[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
257impl<Idx: fmt::Debug> fmt::Debug for RangeInclusive<Idx> {
258 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
259 self.start.fmt(fmt)?;
260 fmt.write_fmt(format_args!("..="))write!(fmt, "..=")?;
261 self.last.fmt(fmt)?;
262 Ok(())
263 }
264}
265
266impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
267 #[inline]
289 #[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
290 #[rustc_const_unstable(feature = "const_range", issue = "none")]
291 pub const fn contains<U>(&self, item: &U) -> bool
292 where
293 Idx: [const] PartialOrd<U>,
294 U: ?Sized + [const] PartialOrd<Idx>,
295 {
296 <Self as RangeBounds<Idx>>::contains(self, item)
297 }
298
299 #[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
321 #[inline]
322 #[rustc_const_unstable(feature = "const_range", issue = "none")]
323 pub const fn is_empty(&self) -> bool
324 where
325 Idx: [const] PartialOrd,
326 {
327 !(self.start <= self.last)
328 }
329}
330
331impl<Idx: Step> RangeInclusive<Idx> {
332 #[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
347 #[inline]
348 pub fn iter(&self) -> RangeInclusiveIter<Idx> {
349 self.clone().into_iter()
350 }
351}
352
353#[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
354#[rustc_const_unstable(feature = "const_range", issue = "none")]
355const impl<T> RangeBounds<T> for RangeInclusive<T> {
356 fn start_bound(&self) -> Bound<&T> {
357 Included(&self.start)
358 }
359 fn end_bound(&self) -> Bound<&T> {
360 Included(&self.last)
361 }
362}
363
364#[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
371#[rustc_const_unstable(feature = "const_range", issue = "none")]
372const impl<T> RangeBounds<T> for RangeInclusive<&T> {
373 fn start_bound(&self) -> Bound<&T> {
374 Included(self.start)
375 }
376 fn end_bound(&self) -> Bound<&T> {
377 Included(self.last)
378 }
379}
380
381#[unstable(feature = "range_into_bounds", issue = "136903")]
383#[rustc_const_unstable(feature = "const_range", issue = "none")]
384const impl<T> IntoBounds<T> for RangeInclusive<T> {
385 fn into_bounds(self) -> (Bound<T>, Bound<T>) {
386 (Included(self.start), Included(self.last))
387 }
388}
389
390#[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
391#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
392const impl<T> From<RangeInclusive<T>> for legacy::RangeInclusive<T> {
393 #[inline]
394 fn from(value: RangeInclusive<T>) -> Self {
395 Self::new(value.start, value.last)
396 }
397}
398#[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
399#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
400const impl<T> From<legacy::RangeInclusive<T>> for RangeInclusive<T> {
401 #[inline]
442 fn from(value: legacy::RangeInclusive<T>) -> Self {
443 if !!value.exhausted {
{
crate::panicking::panic_fmt(format_args!("attempted to convert from an exhausted `legacy::RangeInclusive`"));
}
};assert!(
444 !value.exhausted,
445 "attempted to convert from an exhausted `legacy::RangeInclusive`"
446 );
447
448 let (start, last) = value.into_inner();
449 RangeInclusive { start, last }
450 }
451}
452
453#[lang = "RangeFromCopy"]
481#[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)]
482#[derive_const(#[automatically_derived]
#[rustc_const_unstable(feature = "derive_const", issue = "118304")]
#[stable(feature = "new_range_from_api", since = "1.96.0")]
const impl<Idx: [const] crate::clone::Clone> crate::clone::Clone for
RangeFrom<Idx> {
#[inline]
fn clone(&self) -> RangeFrom<Idx> {
RangeFrom { start: crate::clone::Clone::clone(&self.start) }
}
}Clone, #[automatically_derived]
#[rustc_const_unstable(feature = "derive_const", issue = "118304")]
#[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: &RangeFrom<Idx>) -> bool { self.start == other.start }
}PartialEq, #[automatically_derived]
#[rustc_const_unstable(feature = "derive_const", issue = "118304")]
#[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)]
483#[stable(feature = "new_range_from_api", since = "1.96.0")]
484pub struct RangeFrom<Idx> {
485 #[stable(feature = "new_range_from_api", since = "1.96.0")]
487 pub start: Idx,
488}
489
490#[stable(feature = "new_range_from_api", since = "1.96.0")]
491impl<Idx: fmt::Debug> fmt::Debug for RangeFrom<Idx> {
492 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
493 self.start.fmt(fmt)?;
494 fmt.write_fmt(format_args!(".."))write!(fmt, "..")?;
495 Ok(())
496 }
497}
498
499impl<Idx: Step> RangeFrom<Idx> {
500 #[stable(feature = "new_range_from_api", since = "1.96.0")]
515 #[inline]
516 pub fn iter(&self) -> RangeFromIter<Idx> {
517 self.clone().into_iter()
518 }
519}
520
521impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
522 #[inline]
538 #[stable(feature = "new_range_from_api", since = "1.96.0")]
539 #[rustc_const_unstable(feature = "const_range", issue = "none")]
540 pub const fn contains<U>(&self, item: &U) -> bool
541 where
542 Idx: [const] PartialOrd<U>,
543 U: ?Sized + [const] PartialOrd<Idx>,
544 {
545 <Self as RangeBounds<Idx>>::contains(self, item)
546 }
547}
548
549#[stable(feature = "new_range_from_api", since = "1.96.0")]
550#[rustc_const_unstable(feature = "const_range", issue = "none")]
551const impl<T> RangeBounds<T> for RangeFrom<T> {
552 fn start_bound(&self) -> Bound<&T> {
553 Included(&self.start)
554 }
555 fn end_bound(&self) -> Bound<&T> {
556 Unbounded
557 }
558}
559
560#[stable(feature = "new_range_from_api", since = "1.96.0")]
567#[rustc_const_unstable(feature = "const_range", issue = "none")]
568const impl<T> RangeBounds<T> for RangeFrom<&T> {
569 fn start_bound(&self) -> Bound<&T> {
570 Included(self.start)
571 }
572 fn end_bound(&self) -> Bound<&T> {
573 Unbounded
574 }
575}
576
577#[unstable(feature = "range_into_bounds", issue = "136903")]
578#[rustc_const_unstable(feature = "const_range", issue = "none")]
579const impl<T> IntoBounds<T> for RangeFrom<T> {
580 fn into_bounds(self) -> (Bound<T>, Bound<T>) {
581 (Included(self.start), Unbounded)
582 }
583}
584
585#[unstable(feature = "one_sided_range", issue = "69780")]
586#[rustc_const_unstable(feature = "const_range", issue = "none")]
587const impl<T> OneSidedRange<T> for RangeFrom<T>
588where
589 Self: RangeBounds<T>,
590{
591 fn bound(self) -> (OneSidedRangeBound, T) {
592 (OneSidedRangeBound::StartInclusive, self.start)
593 }
594}
595
596#[stable(feature = "new_range_from_api", since = "1.96.0")]
597#[rustc_const_unstable(feature = "const_index", issue = "143775")]
598const impl<T> From<RangeFrom<T>> for legacy::RangeFrom<T> {
599 #[inline]
600 fn from(value: RangeFrom<T>) -> Self {
601 Self { start: value.start }
602 }
603}
604#[stable(feature = "new_range_from_api", since = "1.96.0")]
605#[rustc_const_unstable(feature = "const_index", issue = "143775")]
606const impl<T> From<legacy::RangeFrom<T>> for RangeFrom<T> {
607 #[inline]
608 fn from(value: legacy::RangeFrom<T>) -> Self {
609 Self { start: value.start }
610 }
611}
612
613#[lang = "RangeToInclusiveCopy"]
656#[doc(alias = "..=")]
657#[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]
fn clone(&self) -> RangeToInclusive<Idx> {
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::cmp::PartialEq for
RangeToInclusive<Idx> {
#[inline]
fn eq(&self, other: &RangeToInclusive<Idx>) -> 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)]
658#[stable(feature = "new_range_to_inclusive_api", since = "1.96.0")]
659pub struct RangeToInclusive<Idx> {
660 #[stable(feature = "new_range_to_inclusive_api", since = "1.96.0")]
662 pub last: Idx,
663}
664
665#[stable(feature = "new_range_to_inclusive_api", since = "1.96.0")]
666impl<Idx: fmt::Debug> fmt::Debug for RangeToInclusive<Idx> {
667 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
668 fmt.write_fmt(format_args!("..="))write!(fmt, "..=")?;
669 self.last.fmt(fmt)?;
670 Ok(())
671 }
672}
673
674impl<Idx: PartialOrd<Idx>> RangeToInclusive<Idx> {
675 #[inline]
689 #[stable(feature = "new_range_to_inclusive_api", since = "1.96.0")]
690 #[rustc_const_unstable(feature = "const_range", issue = "none")]
691 pub const fn contains<U>(&self, item: &U) -> bool
692 where
693 Idx: [const] PartialOrd<U>,
694 U: ?Sized + [const] PartialOrd<Idx>,
695 {
696 <Self as RangeBounds<Idx>>::contains(self, item)
697 }
698}
699
700#[stable(feature = "new_range_to_inclusive_api", since = "1.96.0")]
701impl<T> From<legacy::RangeToInclusive<T>> for RangeToInclusive<T> {
702 fn from(value: legacy::RangeToInclusive<T>) -> Self {
703 Self { last: value.end }
704 }
705}
706#[stable(feature = "new_range_to_inclusive_api", since = "1.96.0")]
707impl<T> From<RangeToInclusive<T>> for legacy::RangeToInclusive<T> {
708 fn from(value: RangeToInclusive<T>) -> Self {
709 Self { end: value.last }
710 }
711}
712
713#[stable(feature = "new_range_to_inclusive_api", since = "1.96.0")]
717#[rustc_const_unstable(feature = "const_range", issue = "none")]
718const impl<T> RangeBounds<T> for RangeToInclusive<T> {
719 fn start_bound(&self) -> Bound<&T> {
720 Unbounded
721 }
722 fn end_bound(&self) -> Bound<&T> {
723 Included(&self.last)
724 }
725}
726
727#[stable(feature = "new_range_to_inclusive_api", since = "1.96.0")]
728#[rustc_const_unstable(feature = "const_range", issue = "none")]
729const impl<T> RangeBounds<T> for RangeToInclusive<&T> {
730 fn start_bound(&self) -> Bound<&T> {
731 Unbounded
732 }
733 fn end_bound(&self) -> Bound<&T> {
734 Included(self.last)
735 }
736}
737
738#[unstable(feature = "range_into_bounds", issue = "136903")]
739#[rustc_const_unstable(feature = "const_range", issue = "none")]
740const impl<T> IntoBounds<T> for RangeToInclusive<T> {
741 fn into_bounds(self) -> (Bound<T>, Bound<T>) {
742 (Unbounded, Included(self.last))
743 }
744}
745
746#[unstable(feature = "one_sided_range", issue = "69780")]
747#[rustc_const_unstable(feature = "const_range", issue = "none")]
748const impl<T> OneSidedRange<T> for RangeToInclusive<T>
749where
750 Self: RangeBounds<T>,
751{
752 fn bound(self) -> (OneSidedRangeBound, T) {
753 (OneSidedRangeBound::EndInclusive, self.last)
754 }
755}