我试图使用petgraph机箱在有向图上实现随机游走。
到目前为止,我已经定义了一个RandomWalk结构,它实现了Walker特性:
extern crate petgraph; // 0.4.13
use petgraph::visit::{GraphBase, Walker};
use petgraph::Direction;
pub struct RandomWalk<G>
where G: GraphBase
{
next: G::NodeId,
}
impl<G> Walker<G> for RandomWalk<G>
where G: GraphBase
{
type Item = G::NodeId;
fn walk_next(&mut self, graph: G) -> Option<Self::Item> {
// Even this deterministic walk does not work:
graph.neighbors_directed(self.next, Direction::Incoming).next()
}
}但是,我得到了错误:
error[E0599]: no method named `neighbors_directed` found for type `G` in the current scope
--> src/lib.rs:50:11
|
50 | graph.neighbors_directed(self.next, Direction::Incoming).next()
| ^^^^^^^^^^^^^^^^^^
|
= note: the method `neighbors_directed` exists but the following trait bounds were not satisfied:
`&G : petgraph::visit::IntoNeighborsDirected`
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `neighbors_directed`, perhaps you need to implement it:
candidate #1: `petgraph::visit::IntoNeighborsDirected`我真的不明白petgraph API是如何工作的,GraphBase不是正确的类型吗?
发布于 2018-10-18 12:54:55
如果您理解编译器,那么解决方案是非常清晰的。
锈蚀不假定任何关于类型的东西,除非您指定它,例如,您不能将两种类型的T一起添加,除非实现Add特性。然后你可以写T + T。
你的问题很相似。
您正在尝试使用函数neighbors_directed,它不是为G实现的(在您的示例中绑定到GraphBase )。相反,您必须指定G还必须通过将特征IntoNeighborsDirected添加到impl块来实现它。
impl<G> Walker<G> for RandomWalk<G> where G: GraphBase + IntoNeighborsDirected这将告诉编译器,G已经实现了neighbors_directed方法,您可以使用它(游乐场)。
https://stackoverflow.com/questions/52873390
复制相似问题