I don't understand why this code doesn't work.
Please help.
Code:
#[derive(Clone, Debug)]
struct Element {
name: String,
foo: String,
}
struct Config {
config_elements: Vec<Element>,
}
impl Config {
fn new() -> Config
{
return Config{config_elements : Vec::new() };
}
}
struct MyStruct {
cfg : Config
}
impl MyStruct {
fn new() -> MyStruct
{
let cs1_e1 = Element {
name: "cs1_e1".to_string(),
foo: "xxx".to_string(),
};
let cs1_e2 = Element {
name: "cs1_e2".to_string(),
foo: "yyy".to_string(),
};
let mut cfg = Config {
config_elements: Vec::new(),
};
cfg.config_elements.push(cs1_e1);
cfg.config_elements.push(cs1_e2);
return MyStruct{cfg : cfg};
}
fn process(&mut self, e : &Element)
{
}
}
pub trait TestTrait {
fn test(&mut self);
}
impl TestTrait for MyStruct {
fn test(&mut self)
{
for e in &self.cfg.config_elements
{
self.process(e);
}
}
}
fn main()
{
let c = MyStruct::new();
}
error[E0502]: cannot borrow `*self` as mutable because it is also borrowed as immutable --> src/main.rs:61:10 | 59 | for e in &self.cfg.config_elements | ------------------------- | | | immutable borrow occurs here | immutable borrow later used here 60 | { 61 | self.process(e); | ^^^^^^^^^^^^^^^ mutable borrow occurs here
Rust Playground:
Click me