Hi
So I have code like
struct Pipeline<'a> {
name : String,
elements : Vec<&'a Element>,
}
impl<'a> Pipeline<'a> {
pub fn new(name: String) -> Self { Pipeline{ name : name, elements : Vec::new(), data_queue : Vec::new(), } }
pub fn add_element(&mut self, element : &'a Element) -> PipelineResult<&'a Element> { let found_element = (&self).find_element(element.get_name()); match found_element { Some(found_element) => { debug!("Element with that name already exits in pipeline"); return Err(PipeLineError::ELEMENT_ALREADY_EXISTS) } None => { self.elements.push(element); return Ok(element) } }; }
pub fn find_element(&self, name : &str) -> Option<&&Element> { return self.elements.iter().find(|&&e| e.get_name() == name); }
}
This gives the error
error[E0502]: cannot borrow
self.elements
as mutable becauseself
is also borrowed as immutable
--> src/lib.rs:151:17
|
141 | let found_element = (&self).find_element(element.get_name());
| ---- immutable borrow occurs here
...
151 | self.elements.push(element);
| ^^^^^^^^^^^^^ mutable borrow occurs here
...
155 | }
| - immutable borrow ends here
Now I can understand the error but I just don't know how to solve this situation .
What's the usual way of dealing with this ?
Thanks