younger

Cannot borrow as mutable because it is also borrowed as immutable

匿名 (未验证) 提交于 2019-12-03 02:28:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am learning Rust and I don't quite get why this is not working. #[derive(Debug)] struct Node { value: String, } #[derive(Debug)] pub struct Graph { nodes: Vec<Box<Node>>, } fn mk_node(value: String) -> Node { Node { value } } pub fn mk_graph() -> Graph { Graph { nodes: vec![] } } impl Graph { fn add_node(&mut self, value: String) { if let None = self.nodes.iter().position(|node| node.value == value) { let node = Box::new(mk_node(value)); self.nodes.push(node); }; } fn get_node_by_value(&self, value: &str) -> Option<&Node> { match self