Allow `use` inside `match`.
Nobody has claimed this yet.
- Dominant language
- Markdown
- Stars
- 6.6k
- Forks
- 1.7k
- Avg merge
- 16h 14m
- Merged PRs (30d)
- 1
Description
Very simply, allow use to take the place of a match arm, for a tightly-scoped import. Intended primarily for work with enums, consider the following:
enum Direction {
North,
South,
East,
West,
}
impl Direction {
fn abbreviation(&self) -> char {
...
}
}
What goes in the ...? Without use, we have to do this:
match self {
Direction::North: "N",
Direction::South: "S",
Direction::East: "E",
Direction::West: "W",
}
This is a lot of repetition and awful for long enums. We can use Direction::*;, but where do we put it? We can just put it outside:
use Direction::*;
match self {
North => "N",
South => "S",
East => "E",
West => "W",
}
But unfortunately this pollutes the rest of the function body. In this toy example, it's not that big of a deal, as we're immediately returning a value anyway. In a longer function, this could lead to less clarity or even a collision between enums with identically-named members. I've seen a scope recommended:
{
use Direction::*;
match self {
... // as above
}
}
But this is kind of ugly and is even moreso if you are trying to use match as an expression in a bigger statement, where you might have to write:
let abbrev = {
use Direction::*;
match {
... // as above
}
}
Instead, I propose the following simple addition to the syntax:
match self {
use Direction::*,
North => "N",
... // continued as above
}
Unless I'm quite mistaken, this is syntactically unambiguous, and while it may seem weird to use a comma rather than a semicolon after a use, there's no fundamental reason we can't put a use here, since they don't have any ordering behaviour anyway. It could desugar to the previous examples with a surrounding block, but the result would be IMO nicely ergonomic. It would also give the option of putting nested enums' use declarations directly before they're actually used in a big switch, where collisions aren't a risk.
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
The issue contains the proposed match/use syntax and desugaring examples, but names no implementation files or tests. Start by reviewing the issue and the repository's RFC process; done means reaching and documenting a decision on the proposed language change.
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
- Clearly specified
- Newbie friendliness
- 25/100