新手问题:我想用花瓣锈病箱。图形应该是struct MyGraph的一部分。为此,我需要包装一些函数以使它们可以从外部访问:
use petgraph::graph::DiGraph;
use petgraph::adj::NodeIndex;
#[derive(Default)]
struct MyNode {}
#[derive(Default)]
struct MyGraph {
g: DiGraph<MyNode,()>
}
impl MyGraph {
pub fn add_node(&mut self, node: MyNode) -> NodeIndex {
self.g.add_node(node) // does not compile: return value is u32 != NodeIndex
}
pub fn update_edge(&mut self, a: NodeIndex, b: NodeIndex) {
self.g.update_edge(a, b, ()); // does not compile: a: NodeIndex expected, got u32
}
}
fn main() {
let mut my_graph=MyGraph::default();
let node1=my_graph.add_node(MyNode::default());
let node2=my_graph.add_node(MyNode::default());
my_graph.update_edge(node1,node2);
}但是,生锈编译器会抱怨u32由self.g.add_node()函数返回,如果您直接调用该函数(并且记录了返回NodeIndex),情况就不一样了。smae问题适用于self.g.update_edge()函数。这里使用的正确参数类型是什么?
返回类型NodeIndex也不起作用。(部分)编译错误消息:
...
error[E0308]: arguments to this function are incorrect
--> src\main.rs:17:16
|
17 | self.g.update_edge(a, b, ());
| ^^^^^^^^^^^
|
note: expected struct `NodeIndex`, found `u32`
--> src\main.rs:17:28
|
17 | self.g.update_edge(a, b, ());
| ^
= note: expected struct `NodeIndex`
found type `u32`
...我在这里做错了什么?
发布于 2022-11-25 12:14:43
您正在导入petgraph::adj::NodeIndex,它是u32的别名,但您需要导入的是petgraph::graph::NodeIndex,它是一个不同的结构。
https://stackoverflow.com/questions/74572416
复制相似问题