一次不能多次借用可变的+变量是可变队列

问题描述 投票:0回答:0

我正在尝试遍历一个图并根据我的代码中定义的标准从中提取一个子树。图形或标准的细节并不重要。然而,由于 Rust 中引用和借用的复杂性,创建树的过程非常具有挑战性。我不是生锈专家。

为了简单起见,我有两个函数:

  1. 第一个获取图,图的深度遍历并返回一个 ScriptTree 类型的子树,它是一个结构。
fn run_subsequent_scripts(&self, graph: &PageGraph, depth: usize) ->  ScriptTree {
    let mut level: usize;
    let mut visited: HashMap<NodeId, bool> = HashMap::new();
    // create a queue for BFS
    let mut queue = vec![];
    // Starting vertex maked as visited and added to queueu
    let script_info = get_script_info(graph, self.query.id);
    let mut root = ScriptTree::new(script_info.0 ,script_info.1, script_info.2);
    
    visited.insert(self.query.id, true);
    queue.push(&mut root);
    
    let mut level_meter: Vec<NodeId> = Vec::new();
    
    // continue until queue is empty
    while queue.len() != 0 && level_meter.len() < depth {
        level = queue.len();

        while level != 0 {
          // Get the front of the queue and remove it
          let mut child_node: ScriptTree;
          let parent_node = queue.remove(0);
          level -= 1;
          // Get all adjacent vertices from that vertex
          // neighbors of the parent
          let neighbors: Vec<NodeId> = get_injected_scripts(&graph,    parent_node.script_info.script_node_id, &Action::script());
          let mut neighbor_nodes: Vec<ScriptTree> = Vec::new();
          for child_id in neighbors {
              if !visited.contains_key(&child_id) {
              //mark as visited
              visited.insert(child_id, true);
              // push back to check this vertex's vertices
              let child_script_info = get_script_info(graph, child_id);
              child_node = ScriptTree::new(child_script_info.0, child_script_info.1, child_script_info.2);
              queue.push(&mut child_node);
              neighbor_nodes.push(child_node);
              level_meter.push(child_id);
              }
          }
        self.update_tree(&mut root, &parent_node, neighbor_nodes);
        }
        level += 1;
    } 
  return root
  }
  1. 第二个函数通过添加在前一个函数中向量化的 node_neighbors 中捕获的 parent_node 的子节点来更新树:
fn update_tree(&self, subtree: &mut ScriptTree, parent_node: &ScriptTree, node_neighbors: Vec<ScriptTree>) {
    if subtree.node_id == parent_node.node_id {
      // Return a reference to this node if it has the target ID
      for child in node_neighbors{
        subtree.add_child(child);
      }
    }
    else {

      // Recursively search the children of this node
      for mut child in &subtree.children {
          self.update_tree(&mut child, parent_node, node_neighbors);
      }
    }    
  }

这段代码给我多行错误:

在功能1中,

  • child_node = ScriptTree::new(child_script_info.0, child_script_info.1, child_script_info.2);
    行它说“不能分配给
    child_node
    因为它是借来的分配给借来的
    child_node
    发生在这里”。
  • queue.push(&mut child_node);
    行中它说“不能一次多次借用
    child_node
    因为可变
    child_node
    在循环的前一次迭代中被可变地借用在这里”
  • neighbor_nodes.push(child_node);
    行它说“不能搬出
    child_node
    因为它是借来的搬出
    child_node
    发生在这里”
  • 并且在
    self.update_tree(&mut root, &parent_node, neighbor_nodes);
    行中它说“不能借用
    root
    一次以上一次可变第二次可变借用发生在这里”

在功能2中:

  • 在线
    self.update_tree( &mut child, parent_node, node_neighbors);
    它说:
  1. 不能借用
    &
    引用中的数据作为可变的不能借用为可变的
  2. 移动值的使用:
    node_neighbors
    值移动到这里,在循环的前一次迭代中

我感谢任何解决问题的帮助!

rust reference tree borrow-checker mutable
© www.soinside.com 2019 - 2024. All rights reserved.