struct Tree {
pub key: String,
pub children: Vec<Tree>
}
impl Tree {
pub fn find(&mut self, key: &str) -> &mut Self {
let result = self.children.iter_mut().find(|k| k.key.eq(key));
match result {
Some(value) => value,
None => {
let new = Tree {
key: key.to_owned(),
children: vec![],
};
self.children.push(new);
let len = self.children.len();
&mut self.children[len - 1]
}
}
}
}
fn main() {
let mut t = Tree {
key: String::new(),
children: vec![]
};
let _new = t.find("a");
}
In my understanding, if the find
method returns None
, there should be no actual reference to self
, and an error is reported here, borrow mutable more than once
. How to fix