我在 Go 中有以下代码,我想用 Rust 重写:
func New(l *lexer.Lexer) *Parser {
p := &Parser{l: l}
// Read two tokens, so curToken and peekToken
// are both set
p.nextToken()
p.nextToken()
return p
}
func (p *Parser) nextToken() {
p.curToken = p.peekToken
p.peekToken = p.l.NextToken()
}
在 Rust 中,我有如下内容:
struct Parser {
l: lexer::Lexer,
cur_token: lexer::Token,
peek_token: lexer::Token,
}
impl Parser {
fn new(l: lexer::Lexer, cur_token: lexer::Token, peek_token: lexer::Token) -> Self {
Self {
l: l,
cur_token: cur_token,
peek_token: peek_token,
}
}
fn init(&mut self) {
self.next_token();
self.next_token();
}
fn next_token(&mut self) {
self.cur_token = self.peek_token;
self.peek_token = self.l.next_token();
}
}
这是用 Rust 编写代码的最佳方式吗?