Pattern bindings of structure variant fields
Nobody has claimed this yet.
- Dominant language
- Markdown
- Stars
- 6.6k
- Forks
- 1.7k
- Avg merge
- 16h 14m
- Merged PRs (30d)
- 1
Description
Currently, the following is a syntax error:
enum MyEnum {
StructVariant {
a: String,
b: u64,
},
}
fn matcher(e: MyEnum) {
match e {
MyEnum::StructVariant v@{ .. } => {
// do something with `v.a` and `v.b`.
},
}
}
error: expected one of `::`, `=>`, `if`, or `|`, found `v`
--> src/lib.rs:10:31
|
10 | MyEnum::StructVariant v@{ .. } => {
| -^ unexpected token
| |
| expected one of `::`, `=>`, `if`, or `|` here
(Side note, why are { and ( not listed in the expected list?)
When a struct variant has many fields, many of which get used in some match arm, the arm tends to get unwieldy very quickly without resorting to trs vrbl nms.
I also want to avoid making it a 1-tuple variant over another struct because that means giving a name to the structure, exporting it, and documenting it ( andsince its only purpose is to give the variant something capture-able, the struct docstring will basically be either a duplicate or "see this variant").
Open questions (with some gut-level answers of my own):
- What is the type of
vabove? IMO, it should be unnameable (like a closure), but still potentially implement default traits such asClone,Copy,Sync, andSend(though another thought makes me think: who can receive this unnamed type other than by a trait object)? This means it probably cannot implementAnyas well. - Can it be returned out of the
matchorif let? I think so. It's still an unnameable type, so it cannot be returned other than throughimpl TraitorBox<Trait>. - Is this a syntax ambiguity? I'm not seeing an obvious one at least.
- Is it applicable to tuple variants as well?
Thoughts?
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the struct-variant match example and the syntax error shown at src/lib.rs:10. Resolve the listed questions about the binding's type, returnability, syntax ambiguity, and tuple variants; done means the proposed behavior and syntax have a settled RFC direction.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100