-1
重構此解析器代碼以避免借用檢查問題的最佳方法是什麼?重構解析器代碼以避免借用檢查問題
pub type Token=u8;
pub trait Stream {
type Item;
fn next(&mut self) -> Option<&Self::Item>;
fn peek(&mut self) -> Option<&Self::Item>;
}
#[derive(Clone)]
pub struct Parser {
input: Vec<Token>,
position: usize,
}
pub type ParseError = String;
pub type ParseResult<T> = Result<T, ParseError>;
impl Stream for Parser {
type Item = Token;
fn next(&mut self) -> Option<&Token> {
let token = self.input.get(self.position);
self.position += 1;
token
}
fn peek(&mut self) -> Option<&Token> {
self.input.get(self.position + 1)
}
}
fn parse_expr(stream: &mut Parser) -> ParseResult<()> {
match stream.peek() { // ~~ borrowed stream here
None => Err(String::from("No more tokens")),
Some(t) => match t {
&0 => parse_number_literal(stream), // ~~ and here
&1 => panic!("parse string"),
&2 => panic!("parse character"),
_ => Err(String::from("Unexpected Token")),
}
}
}
fn parse_number_literal(stream: &mut Parser) -> ParseResult<()> {
let token = stream.next();
Ok(())
}
fn main(){}
編譯器的投訴約cannot borrow
*流as mutable more than once at a time
。閱讀其他stackoverflow問題只回答爲什麼發生這個問題,但不是如何解決問題。
謝謝。將代碼重構爲單一匹配。 –