-
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
Allow use
inside match
.
#2830
Comments
If this were to be done I'd prefer to just see it on the first one instead, it obviously knows the scope for the rest by then: match self {
Direction::North => "N",
South => "S",
East => "E",
West => "W",
} No |
@OvermindDL1 That could be confusing, as arms with just (not-in-scope) identifiers are currently a catch-all. I wouldn't be surprised if that broke code, either. |
@OvermindDL1 that seems hard to do, imagine: match (a, b) {
(Direction::North, Something::Else) => …,
} It would mean to apply that rule recursively to tuples / structs. |
There is visual inconsistency in @OvermindDL1 suggestion because Rust favors explicit placeholders like
I think either the I also think enum variants shadow one another rarely enough that one extra braces layer works okay:
As so often, the underlying driver for syntax changes appears to be rustfmt's bad formatting. Just write so that expressions and items are grouped by purpose, and don't use rustfmt, problem solved. That's said I'd use either of these forms if one ever stabilized. |
I really like the https://docs.swift.org/swift-book/LanguageGuide/Enumerations.html enum CompassPoint {
case north
case south
case east
case west
}
var direction = CompassPoint.west
direction = .east // Type is known here so we can omit In Rust this would look like enum CompassPoint {
North,
South,
East,
West
}
let mut direction = CompassPoint::West;
direction = _::East; Another example with match match point {
_::East => { ... }
_::West => { ... }
_ => { ... }
} |
@Tyg13 how does it work if the |
@kennytm Apologies, this is what I get for posting so late at night. Indeed, it's the type of the value being matched upon that is relevant here, not the value returned from the match. Still, the point stands -- what value does the placeholder bring? |
You need to some indicator to distinguish between enum variants and other items. #[derive(PartialEq, Eq)]
enum CompassPoint { N, E, S, W }
const E: CompassPoint = CompassPoint::S; // :trollface:
use CompassPoint::N as W;
match point: CompassPoint {
E => { /* where am i? */ }
W => { /* where am i? */ }
_ => {}
} |
@kennytm, I was not aware that was possible, but it certainly makes a good case for |
The let foo: Vec<_> = iterator.collect(); |
If that's accepted would this work? fn function(cp: CompassPoint) {}
let direction = _::West;
function(direction); |
I don't see a reason why it wouldn't. It really should have its own thread, though. |
There is a new This is a handy feature and it fits very well in the Rust's type inference. Indeed, consistently specifying enum type in |
Pre-RFC for |
Very simply, allow
use
to take the place of amatch
arm, for a tightly-scoped import. Intended primarily for work with enums, consider the following:What goes in the
...
? Withoutuse
, we have to do this: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: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:
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:Instead, I propose the following simple addition to the syntax:
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 ause
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.The text was updated successfully, but these errors were encountered: