closure requires unique access

て烟熏妆下的殇ゞ 提交于 2019-12-05 23:10:32

You should be able to fix it like this (something you called "satisfy the borrow checker"):

fn add_raw(&mut self, pair: RawLinkPair) {
    let (parent, child) = {
        let convert = |raw: RawLink| {
            Link{
                id:         self.get_or_create(raw.name).id,
                flow:       raw.flow,
            }
        };

        (convert(pair.parent.clone()), convert(pair.child.clone()))
    };
    self.link_concepts(parent, child);
}

As far as I know, this is something inexpressible in current Rust directly (i.e. without closures). Closures take their environment through &only reference (currently it is only available to compiler), which forbids taking other references until it goes out of scope. It is like &mut reference but not necessarily tied to mutability. Closure environment capture happens upon its creation, so lifetime of this capture extends to the end of the function, hence you're getting the error.

Why closures should take their environment by unique reference I don't know, though.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!