rust – 如何使用&mut self从同一类型的另一个方法中调用方法?

我有一个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))
            }
        }
    }        
}

但是,正如您可能怀疑的那样,扫描仪的状态将发生变化,并且需要在不同的状态下以不同的方式处理令牌.因此,从scan()方法本身调用Scanner上的私有方法会很有用,类似于:

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进行可变引用,因为它推送,并且推送()在Vec上需要可变性.那么我的问题就是前面的知识,补救这种情况的理想方法是什么?

我只需要将scan()写为长方法吗?

最佳答案
我的建议是让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)
    })
}

这样可以避免完全借用,也更容易组合.

转载注明原文:rust – 如何使用&mut self从同一类型的另一个方法中调用方法? - 代码日志