-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC for inclusive ranges with ... #1192
Conversation
Imo we should try providing a |
|
||
This struct implements the standard traits (`Clone`, `Debug` etc.), | ||
but, unlike the other `Range*` types, does not implement `Iterator` | ||
directly, since it cannot do so correctly without more internal |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you clarify why more state is needed? Why not just increment start while start is less than or equal to end?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gsingh93 The problem is that 0...u8::MAX
has 257 states: Some(i) for all i; and None.
So you can't modify only one bound and get all the states. You have two options:
- extra flag for "inclusive case yielded"
- do a checked_add on each step, and if it fails then decrement the maximum instead of increment the minimum (symmetric logic applies for
next_back
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so couldn't RangeInclusive simply get another field that is set during desugaring?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I discussed it with a few people while writing the API, but got such negative feedback that I didn't even think to put it as an alternative. However, several people seem to be in support of it.
@Kimundi VG can still use |
I want inclusive ranges, and I want the syntax to be As for variadics, if the syntaxes turn out to be in conflict in some way, I'd rather have |
Oh, also: I think that |
Agreed with niko, though I'd be interested in investigating the API and performance impact of doing the hack I suggest to avoid a second field. You need to do a check in every step anyway. Might as well fold it into the add, right? Things that come to mind:
|
How common are inclusive ranges? I'm wary of putting a language sugar into places where a programmer benefits from it like once a year. (Library sugar like On the other side, inclusive ranges do improve consistency and completeness of the language, that already has inclusive range patterns and exclusive range expressions. On these grounds, I'd suggest to extend the RFC with half-open inclusive range |
@petrochenkov I agree that this works best if we have half-open inclusive ranges and exclusive range patterns as well. If we didn't already have inclusive patterns, I would prefer a method too. |
CC #947 |
Yeah LGTM. |
I've updated the RFC to include |
Hear ye, hear ye. This RFC is entering final comment period. |
@huonw looks good to me. |
I realize this problem already exists with patterns, but I don't like that
Therefore I favor |
I think it's too similar to |
The libs team discussed this at triage today, and our conclusion was that we're ok with the structures defined here. The best route forward is probably to leave the |
Will slices and vectors be indexed with inclusive ranges? |
@bluss not sure; definitely not initially, and seems like that would be best be considered/proposed as a separate RFC. |
Slice use of inclusive ranges seems to be an aspect that needs to be covered as well. Without it, this RFC adds an inconsistency. If we don't want to allow |
Heh, it didn't occur to me that this RFC wouldn't include slicing of slices/vectors with inclusive ranges. I do think we should support it, I think it'll be weird to have inclusive ranges but not support them in the area that is probably most commonly used, even if it eventually comes later. It feels to me like it should be a complete package. |
@rkjnsn +1 for a...b Consistency with match patterns for both .. and ... will result in the least surprising behavior |
You are just quibbling. Yes 99999999 vs 999999999 is not a significant difference, so does |
+1 for ... as inclusive. |
While I agree that the current proposal may lead to confusion because of the small visual difference, I really don't think there's any way around this unless patterns are changed as well. Otherwise, the resulting syntax asymmetry is simply much worse. |
I think the fact that we already have |
My feeling is that we already had this debate back when we adopted |
Huzzah! The language design subteam has decided to accept this RFC. There were two major points of contention:
|
Awesome. |
So, after the fact (sorry if this is way to late but this hasn't been stabalized yet)... How about using an enum to represent enum RangeInclusive<T> {
Empty,
NonEmpty {
start: T,
end: T,
}
} Rational:
|
This PR implements [RFC 1192](https://github.com/rust-lang/rfcs/blob/master/text/1192-inclusive-ranges.md), which is triple-dot syntax for inclusive range expressions. The new stuff is behind two feature gates (one for the syntax and one for the std::ops types). This replaces the deprecated functionality in std::iter. Along the way I simplified the desugaring for all ranges. This is my first contribution to rust which changes more than one character outside of a test or comment, so please review carefully! Some of the individual commit messages have more of my notes. Also thanks for putting up with my dumb questions in #rust-internals. - For implementing `std::ops::RangeInclusive`, I took @Stebalien's suggestion from rust-lang/rfcs#1192 (comment). It seemed to me to make the implementation easier and increase type safety. If that stands, the RFC should be amended to avoid confusion. - I also kind of like @glaebhoerl's [idea](rust-lang/rfcs#1254 (comment)), which is unified inclusive/exclusive range syntax something like `x>..=y`. We can experiment with this while everything is behind a feature gate. - There are a couple of FIXMEs left (see the last commit). I didn't know what to do about `RangeArgument` and I haven't added `Index` impls yet. Those should be discussed/finished before merging. cc @gankro since you [complained](https://www.reddit.com/r/rust/comments/3xkfro/what_happened_to_inclusive_ranges/cy5j0yq) cc #27777 #30877 #1192 rust-lang/rfcs#1254 relevant to #28237 (tracking issue)
I rather missed the boat on this alternative syntax suggestion: |
Rendered