-
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
Syntax for RangeFull: ..
#702
Conversation
Previously discussed here as well as discussions in #rust-internals. It has come up again due to deref coercions enabling I have already implemented this (draft impl) (on top of the PR rust-lang/rust#21374) it is a very simple change to make. dgrunwald's PR (Note: that has been merged) is also relevant because it fixes some grammar issues around the range syntax. cc @gankro. |
I'd also like to add that 👍 from me, makes the language more consistent and opens up cool patterns. |
Note that there may be a few new edge cases. For instance, is |
Yes PLEASE. Thanks @bluss for writing this! I really really really hope the team considers this. It's just much more consistent to have Previously the team seemingly decided against it stating that |
👍 I'm in favor of this. It makes sense since for most use cases we will have an even shorter way of cross borrowing, like &string and &vector. |
👍 I agree with the RFC’s motivation for this: people are now beginning to recognise the use of ranges outside slicing alone: things like @quantheory That particular case would be |
It wasn't stressed enough in the RFC that we will probably want to use ranges a in future libstd API, it's not just about third party crates. |
@quantheory I don't think there are any new grammar edge cases, all the strangeness with this syntax can also be done with the existing @P1start: Without the |
Note that existing code using enum Foo {
A(i32, i32),
B(u32, u32, u64),
}
fn main() {
let k = Foo::A(1, 2);
let q = match k {
Foo::A(..) => 1,
Foo::B(..) => 2,
};
assert_eq!(q, 1);
} Examples:
This RFC should propose an alternate syntax of the above, after |
Very good catch. It doesn't break anything (range syntax is not parsed in patterns, only in expressions), but it's a confusing conflict. We'll have to discuss this issue. |
On patterns, we also have
e.g. #722 wants it. |
Regarding patterns, I think that overlap already exists, it doesn't seem especially bothersome to me. |
Strong +1 here. The consistency argument is compelling enough to me, but the fact that Ranges have identified use cases outside of indexing seals the deal. |
👍, for all of the reasons mentioned above. |
|
I may as well point out that RFC 520 already states that |
Slice patterns are more niche than using |
Maybe. Personally, I've used slice patterns moderately often, because I work with various array-like types a lot. I use wildcards pretty rarely, in part because I typically use a pattern like |
Merged, r = @nikomatsakis, @huonw Tracking issue: rust-lang/rust#21879 RFC: https://github.com/rust-lang/rfcs/blob/master/text/0702-rangefull-expression.md This RFC has been accepted since it is a popular and minor improvement to the range syntax which has multiple use cases. Since we don't accept range syntax in patterns, we don't think there is any reason to worry about a clash of syntax there. |
Implement step 1 of rust-lang/rfcs#702 Allows the expression `..` (without either endpoint) in general, can be used in slicing syntax `&expr[..]` where we previously wrote `&expr[]`. The old syntax &expr[] is not yet removed or warned for.
Regarding range syntax in patterns: we actually do accept ranges, but with I think it would be worth discussing whether we can swap the syntax here: Though the underlying objects / types may be different between Edit: It looks like the different meanings of |
The discussion of inclusive range syntax is taking place, it's here! |
@bluss : Thanks for the link. It seems like Niko explicitly requested not to discuss use of range syntax in patterns in that thread though:
|
Add the syntax
..
forstd::ops::RangeFull
.Range expressions
a..b
,a..
and..b
all have dedicated syntax and produce first-class values. This means that they will be usable and useful in custom APIs, so for consistency, the fourth slicing range,RangeFull
, could have its own syntax..
Rendered Version