-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Made matches! more useful by adding mapping support #79188
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
I think this is too far on the "concise code" path -- I agree that the match to pull out of the option or other enum can be a bit annoying, but I would rather see alternatives (e.g., let else statements) explored in the language itself rather than extensions to cc @withoutboats since I think you've had similar thoughts before, though I could be misremembering |
02e4f82
to
6df807c
Compare
An alternative that doesn't have the binary issue is the match_or macro from the postfix-macros crate. It's usable without postfix notation as well so can be added to the language today. |
I think I also feel that the "implicit Reading I don't think we should do this. A third-party crate could provide such a macro just as easily. I also agree that |
I see there is resistance to this change, so before closing I'll address the arguments made so far and put forward my best arguments.
It seems clear that we already train users to think of
I can understand that perspective, but I do not agree. This additional syntax is consistent with the syntax of
I feel like this option would be an unfortunate and unintuitive downgrade compared to support in As an additional point: Rust has long had a bit of problem when it comes to the usability of enums. There exist additional types like Third-party crates such as While this PR does not specifically address enums, I hope it might act as a precursor to making working with unique enums (rather than nesting |
Shouldn't it be let let maybe_a = my_foo; What if |
@pickfire Would a separate macro named |
Not sure. Since both breaks the control flow such it behaves like |
@pickfire I'm not sure what you mean by 'breaks the flow control'. Conditionally executing inline code is a precedent already set by many features of Rust. |
Ping from triage: |
Do we need a FCP for something like this? |
I'm inclined to agree with @Mark-Simulacrum and @joshtriplett: I think this is too "gadgety" for a std macro. We've tended to have a philosophy of giving the std macros as few "bells and whistles" as possible, and making them do one thing as straightforwardly as possible. A similar case was the (I'll note that if you don't need to bind anything inside the pattern, very soon you will be able to do Thanks for the PR @zesterer; if you want to this could be a great little package on crates.io (and if it sees widespread use, maybe that would shift the balance on including it in std). But I'm currently inclined against adding this API to std. @rfcbot fcp close |
@rfcbot fcp close (no label) |
Team member @withoutboats has proposed to close this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
@withoutboats Thanks for the response. Out of interest, would you be inclined to change your position on this if it were a separate macro named something like |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
On the one hand, I would be more favorable to that, because it would be consistent with my understanding of std's style guidelines for macros. In other words, if we wanted to add this API, that's how I would expect us to add it. But I still wouldn't be inclined to add this functionality to std immediately. |
@withoutboats Thank you. |
The final comment period, with a disposition to close, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. |
This PR adds support for a trailing expression after the pattern in the
matches!
macro.While
matches!
is extremely useful by itself, it does not have the ability to do anything other than perform a binary match.A common pattern in Rust code looks something like the following:
This is rather unwieldy, particularly when used inline. This PR instead allows the following:
Points in favour:
matches!
becomes significantly more useful with this changematch
syntax that this macro emulatesPoints not in favour:
Option
is still binary in nature, it isn't quite so clear that a non-bool
value may be produced