似乎提供了可变变量的不可变变量

时间:2018-09-28 19:19:59

标签: rust ownership mutability borrowing

我有一个可克隆的结构,称为GenICam。该结构具有HashMap个特征对象的Rc<dyn Node>和实现HashMap特征的Rc<Category>结构的Node。密钥为NodeName,它是String的别名。

每个类别都有一个功能名称列表,这些功能名称以nodes的{​​{1}} HashMap表示。应该使用以下功能使用此列表来填充对节点的引用的GenICam字段:

node_list

我收到以下错误:

use std::{collections::HashMap, rc::Rc};

type NodeName = String;

#[derive(Clone)]
pub struct GenICam {
    nodes: HashMap<NodeName, Rc<Node>>,
    categories: HashMap<NodeName, Rc<Category>>,
}

impl GenICam {
    fn get_categories(&self) -> Vec<Rc<Category>> {
        let mut collect = Vec::new();
        for i in &self.categories {
            collect.push(i.1.clone());
        }
        collect
    }

    /// Fills all categories with references to the features if available
    fn populate_categories(&mut self) {
        let mut cats = self.get_categories();
        for cat in cats {
            let mut mutcat = cat;
            mutcat.populate(&self);
        }
    }
}

#[derive(Clone)]
pub struct Category {
    pub name: NodeName,
    features: Vec<String>,
    pub node_list: HashMap<NodeName, Rc<Node>>,
}

impl Category {
    pub fn populate(&mut self, genicam: &GenICam) {
        for feat in &self.clone().features {
            let result = genicam.node(feat.to_string());
            match result {
                None => (),
                Some(x) => self.add_to_node_list(x),
            };
        }
    }

    /// populate the node hashmap with the given node
    pub fn add_to_node_list(&mut self, node: &Rc<Node>) {
        println!("Succes: {:?}", node.name());
        self.node_list.insert(node.name(), node.clone());
    }
}

由于error[E0596]: cannot borrow immutable borrowed content as mutable --> src/genicam.rs:174:4 | | mutcat.populate(&self); | ^^^^^^ cannot borrow as mutable 被定义为mutcat,所以我无法确定为什么let mut是不可变的。

0 个答案:

没有答案
相关问题