Open pitaj opened 1 month ago
Capturing something from a verbal discussion:
In theory, since the new range types aren't iterators directly, and just get turned into iterators, we could automatically handle for i in 10..0
and do the expected thing. However, @eholk pointed out that while that'd be intuitive for integer literals, it'd be surprising for the general m..n
to implicitly do reverse iteration. So, we shouldn't do this.
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..)
.
We discussed this in today's @rust-lang/libs-api meeting, and our conclusion was: the usefulness of .zip(n..)
is high enough that we should make this work. We should ensure that it yields values up to the maximum integer value, and then panics if asked for another value.
We discussed the unresolved questions in the libs-api meeting yesterday.
- The set of inherent methods copied from
Iterator
present on the new range types (Decide which methods to add to newRange
types #125589)
There was a lot of opposition to inherent methods, especially map
and rev
, because they would need to behave differently on a value type than on an iterator. map
would be expected to work like Option::map
and apply a function to the start and end bounds. rev
would be expected to return a range (not an iterator).
However there was some support for adding an iter
inherent method which would be a short-hand for .clone().into_iter()
. We felt that the additional 5 characters of into_iter
(especially the underscore) would make people's code too verbose for such a common operation.
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 other range-related items (like
RangeBounds
) also be moved under therange
module?
We were mostly happy with how things are currently set out in the PR.
- 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..)
It should implement IntoIterator
since, as mentioned, it is useful for things like it.zip(1..)
.
We also discussed the RangeFrom
iterator: the implementation should add a hidden bool
so that any overflow panics happen after returning the maximum integer value, not before. To avoid issues with LLVM's loop optimizations, we may want this bool
to only be used in debug builds: in release builds it may acceptable to allow the iterator to wrap.
- 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()
.
This should be deferred for a future API proposal, only if it turns out there is a need for this.
- Should there be an infallible conversion from legacy to new
RangeInclusive
?
Yes there should be. The case of converting an exhausted RangeInclusive
iterator should be extremely rare in practice, so it's fine to panic in that case.
There was a lot of opposition to inherent methods, especially
map
andrev
, because they would need to behave differently on a value type than on an iterator.
I understand the arguments for purity here, but the immense utility these offer should be taken into account. I'll remove them from the PR, but I think the question should be reconsidered before the edition change is stabilized.
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
andIntoIterator
that are meant to replace the legacy range types, which are!Copy
and implementIterator
directly.Public API
See the RFC for more details.
Steps / History
Unresolved Questions
Iterator
present on the new range types (#125589)std::ops::range::
instead?IterRange
,IterRangeInclusive
or justIter
,IterInclusive
? OrRangeIter
,RangeInclusiveIter
, ...?RangeBounds
) also be moved under therange
module?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..)
range.by_ref().next()
.RangeInclusive
?