Skip to content

Variable labeling of complex match arms should be able to label the whole arm #673

Closed
@steveklabnik

Description

@steveklabnik
Member

Issue by Manishearth
Friday Jun 27, 2014 at 17:41 GMT

For earlier discussion, see rust-lang/rust#15219

This issue was labelled with: B-RFC, I-papercut in the Rust repository


If I have a match arm like this:

0x2A | 0x2D | 0x2E | 0x30 .. 0x39 | 0x41 .. 0x5A | 0x5F | 0x61..0x7A => {}

the following does not work:

a @ 0x2A | 0x2D | 0x2E | 0x30 .. 0x39 | 0x41 .. 0x5A | 0x5F | 0x61..0x7A => {println("{}",a)}
a @ (0x2A | 0x2D | 0x2E | 0x30 .. 0x39 | 0x41 .. 0x5A | 0x5F | 0x61..0x7A) => {println("{}",a)}

In the former example, the a is bound to the first literal only, and in the latter the compiler thinks that it is a tuple (and complains about the lack of commas).

Of course, one can do

a @ 0x2A | a @ 0x2D | a@ 0x2E | a@ 0x30 .. 0x39 /* etc */=> {println("{}",a)}

but that is cumbersome.

Could we get a way to easily bind a variable to the entire match arm?

Activity

mtdowling

mtdowling commented on Apr 13, 2015

@mtdowling

👍. It would be much easier to use complex matching if you can label the whole arm at once.

pnkfelix

pnkfelix commented on Apr 13, 2015

@pnkfelix
Member

Just to be clear ... this is only an issue if you are working with data that is being moved into the arm's pattern, right? (Because otherwise you should just be able to bind the match's input to a variable at the outset, and then use that variable within the arm...)

But maybe I a missing a use-case here.

mtdowling

mtdowling commented on Apr 13, 2015

@mtdowling

That's basically my use case. Having to define a variable outside of the match turns what could be a single expression (e.g., matching on the result of an expression) into a statement and expression.

gsingh93

gsingh93 commented on Apr 16, 2015

@gsingh93

+1, just ran into this.

petrochenkov

petrochenkov commented on Jan 29, 2018

@petrochenkov
Contributor

This is a subset of #1882

added
T-langRelevant to the language team, which will review and decide on the RFC.
on Jan 29, 2018
Centril

Centril commented on Oct 7, 2018

@Centril
Contributor

Closing in favor of rust-lang/rust#54883. We already have pattern_parenthesis for the other part.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-langRelevant to the language team, which will review and decide on the RFC.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @steveklabnik@pnkfelix@mtdowling@Centril@gsingh93

        Issue actions

          Variable labeling of complex match arms should be able to label the whole arm · Issue #673 · rust-lang/rfcs