在枚举选项

时间:2015-05-26 15:59:46

标签: rust borrow-checker

我对Rust很新,所以请不要理解我不懂的东西。 我正在尝试匹配对结构的引用。它的字段op的类型为Option<Operator>,其中Operator是已定义的枚举。这是代码:

fn formatNodeName(node: &ComputeNode) -> String{
    match node.op{
        Some(ref operator) =>   format!("{}[label=\"{}[{}]\\n{}\"];",node.id, node.name, node.id, operator),
        _ =>    format!("{}[label=\"{}[{}]\"];",node.id, node.name, node.id)
    }
}

但是我得到以下错误,我有点理解,有点不理解:

   src/codegen/graph_viz.rs:43:8: 43:12 error: cannot move out of borrowed content
src/codegen/graph_viz.rs:43     match node.op{
                                      ^~~~
src/codegen/graph_viz.rs:44:8: 44:16 note: attempting to move value to here
src/codegen/graph_viz.rs:44         Some(operator) =>   format!("{}[label=\"{}[{}]\\n{}\"];",node.id, node.name, node.id, operator),
                                         ^~~~~~~~
src/codegen/graph_viz.rs:44:8: 44:16 help: to prevent the move, use `ref operator` or `ref mut operator` to capture value by reference
error: aborting due to previous error

如何“正确”实施这样的事情来匹配?

0 个答案:

没有答案