Matching an enum that contains a String #335
-
| I want a rule that should match specifically the value  pub rule element<S: Symbol, B: Backend<S>>(interner: &mut StringInterner<B>) -> Element<S>
    = [Token::Element(e)]
    { let e = interner.get_or_intern(e); Element(e) } // ok
pub rule hydrogen<S: Symbol, B: Backend<S>>(interner: &mut StringInterner<B>) -> Element<S>
    = [Token::Element("H".to_string())] // error here
    { let h = interner.get_or_intern("H"); Element(h) }However, I get the following errors from the compiler: cannot find tuple struct or tuple variant `to_string` in this scope
not found in this scope rustc[E0531]
this pattern has 2 fields, but the corresponding tuple variant has 1 field
expected 1 field, found 2 rustc[E0023]
tok.rs(52, 13): tuple variant has 1 field // this is correct, Token::Element only has 1 fieldHow can I write a rule that specifically matches  | 
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
| The  match token {
    Token::Element("H".to_string()) => { let h = interner.get_or_intern("H"); Element(h) }
     ...
}which produces the same error because the  There's a Rust proposal to make  What you can do on stable Rust right now though is use an  [Token::Element(e) if e == "H"] | 
Beta Was this translation helpful? Give feedback.
The
[]syntax expands into a the pattern of amatchstatement likewhich 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
matchpatterns dereference a String to into astrthat 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
ifguard, which rust-peg passes through onto the match arm. Th…