-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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: destructuring structs, tuple structs and tuples should be consistent #5830
Comments
Thanks for opening this issue, @thestinger! |
Nominating for milestone 2, backwards-compatible |
this is a great issue; i endorse it |
accepted for backwards-compatible milestone |
How about |
Here's a more complete test case. The stuff that's not commented out works (and probably shouldn't), and the stuff that's commented out should. struct Foo(int, int, int, int);
struct Bar{a: int, b: int, c: int, d: int}
fn main() {
let Foo(*) = Foo(5, 5, 5, 5);
//let Bar(*) = Bar{a: 5, b: 5, c: 5, d: 5};
let Bar{_} = Bar{a: 5, b: 5, c: 5, d: 5};
//let (*) = (5, 5, 5, 5);
//let Foo(a, b, *) = Foo(5, 5, 5, 5);
//let Foo(*, d) = Foo(5, 5, 5, 5);
//let (a, b, *) = (5, 5, 5, 5);
//let (*, c, d) = (5, 5, 5, 5);
//let Bar{b: b, *} = Bar{a: 5, b: 5, c: 5, d: 5};
let Bar{b: b, _} = Bar{a: 5, b: 5, c: 5, d: 5};
/*match [5, 5, 5, 5] {
[a, *] => { }
}*/
/*match [5, 5, 5, 5] {
[*, b] => { }
}*/
/*match [5, 5, 5, 5] {
[a, *, b] => { }
}*/
match [5, 5, 5] {
[a, .._] => { }
}
match [5, 5, 5] {
[.._, a] => { }
}
match [5, 5, 5] {
[a, .._, b] => { }
}
} |
Tuples: let Tup(.._) = Tup(1, false);
let (first, second, ..middle, last) = (1, "hi", false, 2, 3.0);
assert_eq!(first, 1);
assert_eq!(second, "hi");
assert_eq!(middle, (false, 2));
assert_eq!(last, 3.0) Structs: let Struct { .._ } = Struct { b: true, s: "hi", i: 23, f: 2.0 };
// Error, 'rest' cannot be named in structs
let Struct { x, ..end } = Struct { b: true, s: "hi", i: 23, f: 2.0 };
let Struct { s: s, i: i, .._ } = Struct { b: true, s: "hi", i: 23, f: 2.0 };
assert_eq!(s, "hi");
assert_eq!(i, 2.0);
let Struct { b, s, .._, f } = Struct { b: true, s: "hi", i: 23, f: 2.0 };
assert_eq!(b, true);
assert_eq!(s, "hi");
assert_eq!(f, 2.0); |
If we wanted to use |
The semantics of using |
I would also add that for structs its a little ugly, so yeah - not sure. Just an idea. |
If we did go with |
You could just remove the ability you destructure the tail for tuples, and go with: let (first, second, .., last) = ...;
let Struct { s: s, i: i, .. } = ...; Either way, going with |
+1 for For that matter, in vector patterns, we could probably make the |
I'm going to make the existing syntax consistent with |
This replaces `*` with `..` in enums, `_` with `..` in structs, and `.._` with `..` in vectors. It adds obsolete syntax warnings for the old forms but doesn't turn them on yet because we need a snapshot. #5830
Renominating for demilestoning. We now have consistent "ignore the rest of these elements" across all patterns except for ignoring multiple fields in tuples (and tuple-like enum variants). We can always add those parts later (it's a backwards-compatible change), and I'm not sure that 1.0 should block on adding those features. Additionally, I think this bug should be closed in favor of a new one which is up to date with the current state of affairs. |
Switching to P-low because its almost done, and the only thing(s?) remaining are back-compat and low priority for post-1.0. |
Closing in favor of #10365, which is what remains to be done. |
trait_sel: only test predicates w/ no substs r? @ghost changelog: none
* Use matches!() macro to improve readability 1. Use `matches!()` macro in `is_line_comment` and `is_block_comment` to improve readability. 2. Very sightly improve the wording of the doc comment for these two functions. * Update wording on doc comment on is_line_comment()
These are minor nitpicks, but they might as well be fixed before it's too late.
Ignoring all the fields is inconsistent:
It doesn't seem like it's currently possible to ignore all fields of a regular tuple.
I think this would be nicer:
With a struct, you can ignore "the rest", but you can't with a tuple struct or tuple:
This would be more consistent, and similar to destructuring fixed-size vectors:
An underscore would mean ignoring one field.
The text was updated successfully, but these errors were encountered: