I’m still a beginner in learning Rust. I try to convert the super tiny compiler from JS to Rust. I try to follow the JS example as much as possible, but also be as Rust idiomatic as possible. I currently implemented the
tokenizer and the
parser. However I’m very unsure, if my Rust code is “good”. My ported Rust code lives here.
I have several questions and I’d be very happy to get some feedback.
c if c.is_whitespace() => (),the correct way to call methods inside
- I want to
peekinto the next char and if it fits my needs, I want to consume it. For this I currently need to call
Some()even when I know through
peek(here) that my value is correct. Is there a way to get rid if the second
- In my
parserfunction I try to map a vector of tokens to an AST. However I have a hard time to get the values out of my tokens which are modelled as an enum. I use
&Token::Number(ref value @ _) => Ok(Ast::NumberLiteral(value.to_string())),(here) to get my value.
ref value @ _and
value.to_string()seem to be super hacky. Is there a better way to do this?
- I mocked
tokensin my test (here), but they are moved here. Can I re-use the same tokens somehow, so I don’t need to make them again (here)?
Thank you very much!