首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >佩特图的all_simple_paths()函数需要什么类型的注释?

佩特图的all_simple_paths()函数需要什么类型的注释?
EN

Stack Overflow用户
提问于 2021-05-14 07:58:31
回答 1查看 116关注 0票数 2

我正在尝试使用Petgraph的all_simple_paths(),但是我不确定需要什么类型的注释,文档没有给出一个例子。它返回一个impl Iterator<Item = TargetColl>,其中TargetColl: FromIterator<G::NodeId>,但我不知道我应该为它做什么样的注释。

代码语言:javascript
复制
for path in algo::all_simple_paths(&graph, x, y, 0, None) {}
代码语言:javascript
复制
error[E0282]: type annotations needed
   --> lib.rs:10:29
   |
10 | for path in algo::all_simple_paths(&graph, x, y, 0, None) {}
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the element type for this iterator is not specified
EN

回答 1

Stack Overflow用户

发布于 2022-10-10 22:07:53

它应该足够使用了

代码语言:javascript
复制
for path in algo::all_simple_paths::<Vec<_>, _>(&graph, x, y, 0, None) {}

下面是一个有用的例子:

代码语言:javascript
复制
use petgraph::{algo, prelude::*};

fn main() {
    let mut graph = DiGraph::<&str, i32>::new();

    let a = graph.add_node("a");
    let b = graph.add_node("b");
    let c = graph.add_node("c");
    let d = graph.add_node("d");

    graph.extend_with_edges(&[
        (a, b, 1),
        (b, c, 1),
        (c, d, 1),
        (a, b, 1),
        (b, d, 1),
    ]);

    let ways = algo::all_simple_paths::<Vec<_>, _>(&graph, a, d, 0, None)
        .collect::<Vec<_>>();

    assert_eq!(4, ways.len());
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67530907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档