我对 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
这样的事情应该如何“正确”实施以匹配?