-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Tracking issue for ..=
inclusive ranges (RFC #1192) -- originally ...
#28237
Comments
Maybe |
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 think this will open up the way for the new kind of unpredictable errors in future because of a simple typo (.. vs ...). Better if it was .... (4 periods). This way it is less error-prone to the human factor, imo. |
I think rust-lang/rfcs#1592 and rust-lang/rfcs#1582 combined make a case for using the |
I've found this issue because I had off-by-one-dot error in my code when I've meant to use exclusive range. 👎 for the The functionality is useful though, so I'd be happy to have it with a different syntax, e.g. |
Is the syntax for this an open question? Since |
I'd personally prefer the |
I use inclusive ranges a lot for equivalent of C-like for loops The problem with overflow for inclusive ranges looks silly, but in practice I never ran into this problem, because Rust is annoying to use with any type other than Since this syntax is still feature-gated I hope the ship hasn't sailed. |
Considering that swift uses |
I don't have any useful insights, but I'd like for inclusive ranges to exit "experimental" status. As I was going through Rust By Example, I found one snippet that could benefit from this: fn fizzbuzz_to(n: u32) {
for n in 1..n + 1 {
fizzbuzz(n);
}
} |
Up ? 😄 |
I want to write an RFC for |
IMHO ..= looks weird. Swift's approach of ... and ..< looks better to me, because I prefer the ellipsis over two dots - ellipsis stands for omission and we are omitting the numbers between the start and the end of the range. I still think ... and .. was good enough. You have 1 character difference, so the mistake is harder to make than +/- or x/y or whatever. |
Since I misunderstood this myself earlier (and so deleted my comment): Per Rust's RFC process, this proposal has already been reviewed, discussed, and approved in RFC pull request 1192. The present issue tracks the implementation of what was previously decided there. The discussion covered many of the points people are raising here: alternative syntax (including no new syntax), the contrast with Ruby's similar operators, etc. If you feel strongly that the feature should be different, I think you need to take it through the same RFC process, because that's how changes to the language get made. But this issue isn't the place for that. |
@jimblandy maybe we should have @nikomatsakis edit that polite reminder and guidance into the first comment in Really Big Print. 😇 |
@shepmaster That would probably be a good thing to add to a template used to file all tracking issues. |
Nominating for discussion/possible FCP |
We discussed this in the @rust-lang/lang meeting. There was a general sense of unhappiness with this feature -- we considered moving to deprecate, but decided to hold off on that for the time being. There are two major objections to
To that end, we were wondering if someone would be willing to drive forward an RFC that enabled a more general syntax like that let people specify precisely whether the lower- and upper-bounds would be inclusive or exclusive. I think @aturon would be happy to work with someone on such an RFC. |
Inclusive ranges are currently a unstable feature of Rust. '..=' for ranges requires #![feature(inclusive_range_syntax)] '..=' for range patterns requires #![feature(dotdoteq_in_patterns)] The nightly version of Rust does not accept the previous syntax '...' for inclusive ranges anymore. (For range patterns the old syntax is still supported) It produces the following output: error: `...` syntax cannot be used in expressions help: Use `..` if you need an exclusive range (a < b) help: or `..=` if you need an inclusive range (a <= b) We still support the old '...' syntax for both ranges and range patterns. See the tracking issue rust-lang/rust/issues/28237 for '..=' inclusive ranges. (RFC 1192) Fixes intellij-rust#2335
Inclusive ranges are currently a unstable feature of Rust. '..=' for ranges requires #![feature(inclusive_range_syntax)] '..=' for range patterns requires #![feature(dotdoteq_in_patterns)] The nightly version of Rust does not accept the previous syntax '...' for inclusive ranges anymore. (For range patterns the old syntax is still supported) It produces the following output: error: `...` syntax cannot be used in expressions help: Use `..` if you need an exclusive range (a < b) help: or `..=` if you need an inclusive range (a <= b) We still support the old '...' syntax for both ranges and range patterns. See the tracking issue rust-lang/rust/issues/28237 for '..=' inclusive ranges. (RFC 1192) Fixes #2335 (cherry picked from commit 6f84a9c)
Stabilize inclusive range (`..=`) Stabilize the followings: * `inclusive_range` — The `std::ops::RangeInclusive` and `std::ops::RangeInclusiveTo` types, except its fields (tracked by #49022 separately). * `inclusive_range_syntax` — The `a..=b` and `..=b` expression syntax * `dotdoteq_in_patterns` — Using `a..=b` in a pattern cc #28237 r? @rust-lang/lang
This provides documentation for rust-lang/rust#28237.
This provides documentation for rust-lang/rust#28237.
|
@mominul a feature is available only after it has been merged into master branch, thus |
That way you can test them on beta and nightly before they get pushed to stable. |
Thanks for the inputs @kennytm and @clarcharr ! Rust 1.26 release is definitely not boring at least for me! 😄 |
I know it's too late to pitch in on this discussion, but why not omit the In Scala you have This both makes the intent more clear, and avoids the "off-by-one" error that everyone seems so concerned about. Granted, this does break all the existing code, if the original syntax doesn't stay supported. |
@ElectricCoffee in rust ranges support all types though, not just numbers. I think we'd need to introduce It works well in Scala, but Rust's design choices differ largely. |
@ElectricCoffee although l would like the feeling of that, I don't think it's desired to add the additional keywords for it. I believe built-in language keywords should be kept at a minimum, as they might collide with function or variable names. Although |
@ElectricCoffee But if |
And you're absolutely right about that @Boscop, it was however just an example of how words could be done. I believe I've seen In Scala, the inclusive range is more used than the exclusive one, and thus is given the shorter word. |
@timvisee One simply can use |
@hyst329 I didn't even think about that. I must say, I really like that idea! You are indeed correct. I don't believe though that this would be a proper full replacement for the current (/new) syntax. But it would be a nice addition. |
I have to agree with Boscop's comment about intuitiveness. Aside from words like "including" and "except", day-to-day English doesn't really distinguish between inclusive and exclusive ranges strongly enough for there to be a ready-made shortcut. Unless it's used in a context where "A through B" is also used, it's ambiguous whether "A to B" means an inclusive or exclusive range in day-to-day speech here in southern Ontario, Canada, and "until" is associated with time strongly enough that, when used in this looser sense, it's unclear whether the "event" that "until" associates with is "until we see X" or "until we've processed X". |
@hyst329 Having them as methods would limit ranges to number types, though. I'd really rather not have one syntax for ranges of numbers and another for ranges of other things. I guess we could add a new catch-all trait to the prelude for creating ranges, but that's still verging on a breaking change for things which have actual |
I confess, I thought the suggestion of keywords was an April fools joke.
The ..= syntax has been stabilized.
…On Thu, Apr 5, 2018 at 1:53 PM, David Ross ***@***.***> wrote:
@hyst329 <https://github.com/hyst329> Having them as methods would limit
ranges to number types, though. I'd really rather not have one syntax for
ranges of numbers and another for ranges of other things.
I guess we could add a new catch-all trait to the prelude for creating
ranges, but that's still verging on a breaking change for things which have
actual to and until methods.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#28237 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAC3nwBj-b985q1Ez0OHDEHkG6DWV_5nks5tlloZgaJpZM4F4LbW>
.
|
Yeah, we've had 300+ comments on this issue, and the very first comment says:
I'm going to lock this issue for now, sorry if I'm stepping on any toes! |
I believe that everything covered by this issue is done. Please reopen and update the checklist in the issue’s original message if there’s something else. |
Current status
We are planning to to change the syntax for inclusive ranges and patterns to
..=
. The...
syntax in patterns is stable and will remain (silently) deprecated for the time being; rustfmt can rewrite...
to..=
. This comes after much discussion. See this comment for justification.No more syntax discussion should be had in this thread. Any different proposals of exclusive range syntax should take place on the user's forum or internals forum, after you have read all existing comments and their rationale here. Notably, breaking backwards compatibility is a non-starter.
Steps to take
..=
as synonym for...
in patterns and to accept..=
in expressions Initial support for..=
syntax #44709RangeInclusive
..=
) #47813RangeInclusive
have been left out from this round of stabilization. Please track Tracking issue forstart
,end
andnew
methods of RangeInclusive #49022 on this feature.The text was updated successfully, but these errors were encountered: