How to call a method using & mut self from another method of the same type?

I have a structure Scannerthat has a method scan(&mut self). It looks like this.

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))
            }
        }
    }        
}

However, as you might suspect, the state Scannerwill change and it will need to process the tokens differently in different states. Therefore, it would be useful to call a private method on Scannerfrom the method itself scan(), akin to this:

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))
    }
}

But wait! We cannot do this, Rust tells us:

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);
                                  ^~~~

However, our method char_match()MUST have a mutable reference to self, as it pushes, and push()ing Vecdoes not require mutability. Then my question received previous knowledge, what is the ideal way to rectify this situation?

scan() ?

+4
2

, 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)
    })
}

, .

+3

char_match self. self.output self.state . self.input, .

+4

Source: https://habr.com/ru/post/1605991/


All Articles