Open
Description
openedon May 28, 2024
Feature gate: #![feature(new_range_api)]
This is a tracking issue for the library additions that are part of RFC 3550: New range types
Tracking issue for the language change: #123741
This feature introduces new types implementing Copy
and IntoIterator
that are meant to replace the legacy range types, which are !Copy
and implement Iterator
directly.
Public API
// core::range
#[derive(Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct Range<Idx> {
pub start: Idx,
pub end: Idx,
}
#[derive(Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct RangeInclusive<Idx> {
pub start: Idx,
pub end: Idx,
}
#[derive(Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct RangeFrom<Idx> {
pub start: Idx,
}
pub struct IterRange<Idx> { <private fields> }
pub struct IterRangeInclusive { <private fields> }
pub struct IterRangeFrom { <private fields> }
impl<Idx: Step> Iterator for IterRange<Idx> { ... }
impl<Idx: Step> Iterator for IterRangeInclusive<Idx> { ... }
impl<Idx: Step> Iterator for IterRangeFrom<Idx> { ... }
impl<Idx: Step> IntoIterator for Range<Idx> {
type IntoIter = IterRange<Idx>;
...
}
impl<Idx: Step> IntoIterator for RangeInclusive<Idx> {
type IntoIter = IterRangeInclusive<Idx>;
...
}
impl<Idx: Step> IntoIterator for RangeFrom<Idx> {
type IntoIter = IterRangeFrom<Idx>;
...
}
See the RFC for more details.
Steps / History
- Implementation: Add
new_range_api
for RFC 3550 #125751 - Optimize implementation
- Final comment period (FCP)1
- Stabilization PR
Unresolved Questions
- Figure out a way to have
IterRangeFrom
overflow panic only after yielding the maximum value, without impacting release mode - How can we / should we optimize
IterRangeInclusive
now that it is not bound by the API constraints of legacyRangeInclusive
? - The set of inherent methods copied from
Iterator
present on the new range types (Decide which methods to add to newRange
types #125589) - The exact module paths and type names
- Should the new types live at
std::ops::range::
instead? IterRange
,IterRangeInclusive
or justIter
,IterInclusive
? OrRangeIter
,RangeInclusiveIter
, ...?
- Should the new types live at
- Should other range-related items (like
RangeBounds
) also be moved under therange
module? - Should
RangeFrom
even implementIntoIterator
, or should it require an explicit.iter()
call? Using it as an iterator can be a footgun, usually people wantstart..=MAX
instead. Also, it is inconsistent withRangeTo
, which doesn't implementIntoIterator
either. But, it's extremely useful forit.zip(1..)
- Should there be a way to get an iterator that modifies the range in place, rather than taking the range by value? That would allow things like
range.by_ref().next()
. - Should there be an infallible conversion from legacy to new
RangeInclusive
?
impl<Idx> From<legacy::RangeInclusive<Idx>> for RangeInclusive<Idx> {
// How do we handle the `exhausted` case?
}
Footnotes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment