Skip to content

Matching an enum that contains a String #335

Answered by kevinmehall
bigyihsuan asked this question in Q&A
Discussion options

You must be logged in to vote

The [] syntax expands into a the pattern of a match statement like

match token {
    Token::Element("H".to_string()) => { let h = interner.get_or_intern("H"); Element(h) }
     ...
}

which produces the same error because the .to_string() is an expression, but not valid as a pattern on the left hand side of a match.

There's a Rust proposal to make match patterns dereference a String to into a str that you can match with a string literal pattern like "H". Looks like it may even be implemented for String already if you turn on #![feature(string_deref_patterns)] on Nightly.

What you can do on stable Rust right now though is use an if guard, which rust-peg passes through onto the match arm. Th…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@bigyihsuan
Comment options

Answer selected by bigyihsuan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants