首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将petgraph Dot写入文件

将petgraph Dot写入文件
EN

Stack Overflow用户
提问于 2019-08-18 06:18:43
回答 1查看 393关注 0票数 3

我可能遗漏了一些非常基本的东西--我是Rust的新手。我正在尝试将petgraph::dot::Dot表示形式写入到文件中。

下面的小代码示例不能编译:

代码语言:javascript
复制
use petgraph::Graph;
use petgraph::dot::{Dot, Config};
use std::fs::File;
use std::io::Write;


fn main() {
    println!("hello graph!");
    let mut graph = Graph::<_, ()>::new();
    graph.add_node("A");
    graph.add_node("B");
    graph.add_node("C");
    graph.add_node("D");
    graph.extend_with_edges(&[
        (0, 1), (0, 2), (0, 3),
        (1, 2), (1, 3),
        (2, 3),
    ]);

    println!("{:?}", Dot::with_config(&graph, &[Config::EdgeNoLabel]));
    let mut f = File::create("example1.dot").unwrap();
    let output = format!("{}", Dot::with_config(&graph, &[Config::EdgeNoLabel]));
    f.write_all(&output.as_bytes());
}

以下是编译器错误输出:

代码语言:javascript
复制
error[E0277]: `()` doesn't implement `std::fmt::Display`
  --> examples/graphviz.rs:21:32
   |
21 |     let output = format!("{}", Dot::with_config(&graph, &[Config::EdgeNoLabel]));
   |                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `()` cannot be formatted with the default formatter
   |
   = help: the trait `std::fmt::Display` is not implemented for `()`
   = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
   = note: required because of the requirements on the impl of `std::fmt::Display` for `petgraph::dot::Dot<'_, &petgraph::graph_impl::Graph<&str, ()>>`
   = note: required by `std::fmt::Display::fmt`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.

petgraph文档指出,我和Dot implements Display trait将我代码基于trait.Display doc中的示例代码

我可以通过将格式字符串更改为{:?}来使代码正常工作,但我认为这只是为了调试。有没有更好的方法来编写代码来完成同样的事情?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-18 06:29:19

仅当边和节点权重都实现Display时,Dot才实现Display

因为您的边是(),所以不能显示此图形。

例如,更改图形声明以使用i32边权重:

代码语言:javascript
复制
    let mut graph = Graph::<_, i32>::new();

使程序编译时没有错误。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57540335

复制
相关文章

相似问题

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