我有一个Scanner
结构,实现了scan(&mut self)
方法。它看起来像这样。
pub struct Scanner {
input: String,
output: Vec<String>,
state: ScannerState,
}
impl Scanner {
pub fn scan(&mut self) {
self.state = ScannerState::CharMode;
for character in self.input.chars() {
match character {
i @ '0'...'9' => self.output.push(format!("Integer digit: {}", i)),
'+' => self.output.push("Addition operator: +".to_string()),
'-' => self.output.push("Subtraction operator: -".to_string()),
'*' => self.output.push("Multiplication operator: *".to_string()),
'/' => self.output.push("Division operator: /".to_string()),
'%' => self.output.push("Modulus operator: %".to_string()),
'^' => self.output.push("Exponent operator: ^".to_string()),
'=' => self.output.push("Assignment operator: =".to_string()),
';' => self.output.push("Statement terminator: ;".to_string()),
c @ 'a'...'z'| c @ 'A'...'Z'
=> self.output.push(format!("Variable name: {}", c)),
' ' => self.output.push("Space, ignoring.".to_string()),
z @ _ => self.output.push(format!("Unrecognized token: {}", z))
}
}
}
}
但是,正如您可能怀疑的那样,Scanner
的状态将发生变化,并且需要在不同的状态下对标记进行不同的处理。因此,从Scanner
方法本身调用scan()
上的私有方法会很有用,类似于:
pub fn scan(&mut self) {
self.state = ScannerState::CharMode;
while self.state == ScannerState::CharMode {
for character in self.input.chars() {
self.char_match(character);
}
self.state = ScannerState::Done;
}
}
fn char_match(&mut self, c: char) {
match c {
'+' => self.output.push("Addition operator: +".to_string()),
'-' => self.output.push("Subtraction operator: -".to_string()),
'*' => self.output.push("Multiplication operator: *".to_string()),
'/' => self.output.push("Division operator: /".to_string()),
'%' => self.output.push("Modulus operator: %".to_string()),
'^' => self.output.push("Exponent operator: ^".to_string()),
'=' => self.output.push("Assignment operator: =".to_string()),
';' => self.output.push("Statement terminator: ;".to_string()),
' ' => self.output.push("Space, ignoring.".to_string()),
'q' => self.state = ScannerState::QuitMode,
i @ '0'...'9' => self.output.push(format!("Integer digit: {}", i)),
c @ 'a'...'z' => self.output.push(format!("Variable name: {}", c)),
z @ _ => self.output.push(format!("Unrecognized token: {}", z))
}
}
但是等等!我们不能这样做,Rust告诉我们:
src/scanner.rs:34:17: 34:21 error: cannot borrow `*self` as mutable because `self.input` is also borrowed as immutable
src/scanner.rs:34 self.char_match(character);
^~~~
然而,我们的char_match()
方法需要对self
提供可变引用,因为它推送,push()
Vec
需要可变性。那么我的问题就是先前的知识,解决这种情况的理想方法是什么?
我是否只需将scan()
写为长方法?
答案 0 :(得分:4)
char_match
不需要对self
的可变访问权限。对self.output
和self.state
的可变访问就足够了。如果您不允许其访问self.input
,编译器将不再抱怨。
答案 1 :(得分:3)
我的建议是让char_match
无国籍:
pub fn scan(&mut self) {
self.state = ScannerState::CharMode;
while self.state == ScannerState::CharMode {
for character in self.input.chars() {
match char_match(character) {
Some(string) => self.output.push(string),
None => self.state = ScannerState::QuitMode
}
}
self.state = ScannerState::Done;
}
}
fn char_match(c: char) -> Option<String> {
Some(match c {
'+' => "Addition operator: +".into(),
'-' => "Subtraction operator: -".into(),
'*' => "Multiplication operator: *".into(),
'/' => "Division operator: /".into(),
'%' => "Modulus operator: %".into(),
'^' => "Exponent operator: ^".into(),
'=' => "Assignment operator: =".into(),
';' => "Statement terminator: ;".into(),
' ' => "Space, ignoring.".into(),
'q' => return None,
i @ '0'...'9' => format!("Integer digit: {}", i),
c @ 'a'...'z' => format!("Variable name: {}", c),
z @ _ => format!("Unrecognized token: {}", z)
})
}
这样可以避免完全借用,而且更容易组合。