Mutable / immutable elements inside a struct

Hi

I have the following struct

/// Struct representing general config
#[derive(Default, Clone)]
  pub struct Akeva {
 /// Environment variable AKEVA_HOME_PATH
     akeva_home_path: String,

 /// Config obtained from config toml
     config: configuration_domain::Config,

  /// Akeva index
     index: Index,

}

impl Akeva {
/// constructor
  fn new() -> Self {
  Akeva {
       akeva_home_path: Akeva::akeva_home_environment(),
       ..Default::default()
  }
}

... 

 /// create phisical files to store the information
    fn create_database_schema(&mut self) {
      self.segregate_database_path();

      let index = self.get_index();
      let data_files_root_path = Path::new(&self.get_akeva_home_path()).join(&self.get_config().path.filesystem_phisical_path);

      if !data_files_root_path.exists() {
         panic!(
              "Couldn't find {} folder . 
                  Please, check configuration value 'filesystem_path_raw' or 
                  create a folder in 'filesystem_path_raw' to store data",
              data_files_root_path.display()
         );
     }

     index.init(&data_files_root_path);
 }

...

 /// getter of the index
 pub fn get_index(&mut self) -> &mut Index { 
     &mut self.index
 }

 /// getter of akeva home path
 pub fn get_akeva_home_path(&self) -> &String {
     &self.akeva_home_path
 }

 /// getter of config
 pub fn get_config(&self) -> &configuration_domain::Config {
     &self.config
 }

}

Each time I perform cargo check I receive the following error :

error[E0502]: cannot borrow *self as immutable because it is also borrowed as
mutable
--> src/lib.rs:92:47
|
91 | let index = self.get_index();
| ---- mutable borrow occurs here
92 | let data_files_root_path = Path::new(&self.get_akeva_home_path()).join(&self.get_config().path.filesystem_phisical_path);
| ^^^^ immutable borrow occurs here
...
103 | index.init(&data_files_root_path);
| ----- mutable borrow later used here

error[E0502]: cannot borrow *self as immutable because it is also borrowed as mutable
--> src/lib.rs:92:81
|
91 | let index = self.get_index();
| ---- mutable borrow occurs here
92 | let data_files_root_path = Path::new(&self.get_akeva_home_path()).join(&self.get_config().path.filesystem_phisical_path);
| ^^^^ immutable borrow occurs here
...
103 | index.init(&data_files_root_path);
| ----- mutable borrow later used here

I know I'm mixing mutable and inmutable references , but I need to modify field Index (so I understand I need getter that returns &mut Index), and the others getters are ok if they're inmutable references . So, I have a getter that returns a mutable borrow, and two the returns inmutable references . But, the problem comes in method create_database_schema, that takes inmutable and mutable references . How can I unwrap this knot ? :slight_smile:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.