Cannot borrow self mutably: it's borrowed immutably for field iteration

I’m trying to build a parser for a project and I’m getting the following error:

error[E0502]: cannot borrow `*self` as mutable because it is also borrowed as immutable
  --> src\compiler\
29 |         let mut iter = self.file.chars();
   |                        ----------------- immutable borrow occurs here
30 |
31 |         while let Some(lexeme) = self.get_lexeme(&mut iter) {
   |                                  ^^^^^^^^^^^^^^^^---------^
   |                                  |               |
   |                                  |               immutable borrow later used here
   |                                  mutable borrow occurs here

My struct looks like this:

pub enum LexemeKind {
    // just some kinds of lexemes, like number or operator

pub struct Lexeme {
    pub content: String,
    pub kind: LexemeKind,

pub struct Lexer {
    pub file: String,
    pub lexemes: Vec<Lexeme>,

impl Lexer {
    pub fn new(file_path: &String) -> Self {
        // just gets file contents and creates empty Vec

    pub fn process(&mut self) { // called in after Lexer::new()
        let mut iter = self.file.chars();                      // error occurs here

        while let Some(lexeme) = self.get_lexeme(&mut iter) {  // and here

    fn get_lexeme(&mut self, iter: &mut Chars) -> Option<Lexeme> {
        if self.file.len() == 0 {
            return None;

        let kind = get_kind(iter.peek().unwrap());
        // checks the kind of the first character

        let content: String = match kind {
            // pattern matching, assigns function to get token based on kind
            // iter is also used as a parameter for these functions

        Some(Lexeme {

    // functions to get entire token of characters with similair type
    // such as: pub fn get_string(&mut self, iter: &mut Chars) -> String
    // they also call file.split_off(index) and the result is returned

fn get_kind(c: char) -> LexemeKind {
    // gets the lexeme kind by checking for opertor, number, brackets, etc.

I’m relatively new to Rust and I’m having trouble resolving the error. Any help would be appreciated.

The error message you’re seeing is caused by a borrowing issue in your code. The issue is that you’re trying to borrow self mutably (&mut self) in the get_lexeme function while it is already borrowed immutably in the process function (let mut iter = self.file.chars();).

To fix this, you can change the process function to make a mutable reference to self.file instead of borrowing self immutably. Here’s the updated code:

pub fn process(&mut self) {
    let mut iter = self.file.chars();

    while let Some(lexeme) = self.get_lexeme(&mut iter) {

With this change, the borrowing issue should be resolved.