We can use Enum::{A, B, C}
, and we can match Enum::A | Enum:: B | Enum::C
-- what if we combine those ideas and allow patterns like Enum::{A | B | C}
?
Here's a real example from pr71231:
PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect)
| PlaceContext::MutatingUse(MutatingUseContext::Store)
| PlaceContext::MutatingUse(MutatingUseContext::AsmOutput)
| PlaceContext::MutatingUse(MutatingUseContext::Borrow)
| PlaceContext::MutatingUse(MutatingUseContext::AddressOf)
| PlaceContext::MutatingUse(MutatingUseContext::Projection)
| PlaceContext::NonMutatingUse(NonMutatingUseContext::SharedBorrow)
| PlaceContext::NonMutatingUse(NonMutatingUseContext::UniqueBorrow)
| PlaceContext::NonMutatingUse(NonMutatingUseContext::ShallowBorrow)
| PlaceContext::NonMutatingUse(NonMutatingUseContext::AddressOf)
| PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection) => {
Using nested or_patterns
, this collapsed a fair bit:
PlaceContext::MutatingUse(
MutatingUseContext::Store
| MutatingUseContext::AsmOutput
| MutatingUseContext::Borrow
| MutatingUseContext::AddressOf
| MutatingUseContext::Projection,
)
| PlaceContext::NonMutatingUse(
NonMutatingUseContext::Inspect
| NonMutatingUseContext::SharedBorrow
| NonMutatingUseContext::UniqueBorrow
| NonMutatingUseContext::ShallowBorrow
| NonMutatingUseContext::AddressOf
| NonMutatingUseContext::Projection,
) => {
But it's still repetitive with those Context
enum names. What if we could write this instead:
PlaceContext::MutatingUse(MutatingUseContext::{
Store | AsmOutput | Borrow | AddressOf | Projection
})
| PlaceContext::NonMutatingUse(NonMutatingUseContext::{
Inspect | SharedBorrow | UniqueBorrow | ShallowBorrow | AddressOf | Projection
}) => {
Or you could brace it even more:
PlaceContext::{
MutatingUse(MutatingUseContext::{
Store | AsmOutput | Borrow | AddressOf | Projection
})
| NonMutatingUse(NonMutatingUseContext::{
Inspect | SharedBorrow | UniqueBorrow | ShallowBorrow | AddressOf | Projection
})
} => {
But I might not choose to go that far in this case due to the added rightward drift. Of course, I'm not certain that this example is formatted how rustfmt would approach it either.
You could also use braced or-patterns when matching with constants, e.g. std::i32::{MIN | MAX}
, but I don't expect such usage would be as common.
There's some overlap in this idea with the thread "Bring enum variants in scope for patterns". In my case, you'd still have to repeat that prefix on separate match arms, but I think it has the advantage of being unambiguous syntax.