Can I use the enum's variant name (without their fields) as a function parameter? I want to iterate through a vector of enum and stop when I found a specific variant. Here is my case:
I have an enum like this:
enum TokenKind {
Ident(String),
Let,
While,
...
}
struct Token {
kind: TokenKind,
pos: Pos,
}
I want to create a parser that parse a VecDeque<Token>
into an AST. In order to do that, I created a helper function like this:
fn skip_until(tokens: &mut VecDeque<Token>, kind: [the TokenKind's variant]) {
while let Some(tok) = tokens.pop_front() {
if [tok.kind's variant matches kind] { // this is not possible
break;
}
}
}
I don't think passing an enum's variant (without their field) is possible, but is there something that I miss? I tried to use macro, but there is another case that I can't solve using macro:
fn consume_next(tokens: &mut VecDeque<Token>, kind: [the TokenKind's variant]) -> Option<Token> {
let Some(tok) = tokens.pop_front() else {
println!("Expected {}, but found EOF", tok.kind);
};
if [tok.kind matches kind] {
return Some(tok);
}
println!("Expected {}, but found {}", kind /* this is not possible, even with declarative macro */, tok.kind);
None
}
``