Comments (4)
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.
from rust.
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.
from rust.
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.
from rust.
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.
Arguments for `map` and `rev`
I think range types are in a very unique position, here. map
and rev
are
- commonly used in the wild
- present in much documentation
- demonstrated in countless learning materials
- a useful entry into iterator chains
While shaving five characters and the shift necessary for the underscore is helpful, the remaining seven characters and two shifts for .iter()
is still a burden and needless visual noise.
My position is that there is no other meaning we could assign to map
that would be acceptable given the history of using it in this position.
map
is expected to map the bounds, not the yielded elements
- It is already a widely used pattern
- Changing
map
to mean "map the bounds" would be a sizeable hazard, because it could silently change the meaning offor (1..5).map(|n| n*2) { ... }
across an edition boundary. - So, a function doing so would need to be named something like
map_bounds
anyways. Range
is not likeOption
orVec
. It's not commonly used to hold values, it's purpose is to generate values.
rev
is expected to return something like RevRange
IMO this argument is stronger, because for x in (1..5).rev()
would still work. That covers one of the most common use-cases. However, I don't see much use for a RevRange
type.
What would it implement besides IntoIterator
?
- not
RangeBounds
- probably not
Index
- could have
is_empty
andcontains
but those are direction-agnostic
It it only serves as an iterable, we may as well just use Rev<IterRange<_>>
anyways.
from rust.
Related Issues (20)
- https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md
- rustdoc-json: Support `unsafe_extern_blocks`
- `#[used]` attribute on static items in `extern` block has suboptimal error message HOT 1
- Build fail for WebAssembly on Windows with wasm-pack HOT 1
- Crater runs for 1.80 HOT 21
- assertion `left == right` failed HOT 1
- PR #113525 (Dynamically size sigaltstk in stdI) adds dependency on `libc::getauxval` breaking linaro-1.13.1-4.7 compatibility HOT 4
- `doc(inline)` overrides `doc(hidden)`, but only for crates HOT 1
- Inline ASM: Registers in the `dreg_low16` register class fail cause compilation failures when the `d32` target feature is disabled HOT 1
- Tracking Issue for `core::clone::CloneToUninit` trait HOT 6
- trying to get pointer to extern typed field causes runtime panic HOT 5
- Tracking Issue for `-Z verbose-asm`
- `unreachable_code` detection and/or lint seems broken HOT 1
- Internal Compiler Error when using extern types HOT 7
- Tracking Issue for consolidating command invocations in bootstrap
- electric bug display with seismic detector HOT 1
- Can `DynMetadata` be made constructable? HOT 1
- ICE: type map: `left == right`
- C-variadic functions may not be extern "C-unwind" HOT 3
- Tracking Issue for `mpmc`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rust.