Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Debugging 如何在Rust中打印变量并让它显示该变量的所有信息,比如Ruby';检查?_Debugging_Rust_Println - Fatal编程技术网

Debugging 如何在Rust中打印变量并让它显示该变量的所有信息,比如Ruby';检查?

Debugging 如何在Rust中打印变量并让它显示该变量的所有信息,比如Ruby';检查?,debugging,rust,println,Debugging,Rust,Println,将无法编译: 错误[E0277]:`std::collections::HashMap`未实现`std::fmt::Display'` -->src/main.rs:6:20 | 6 | println!(“{}”,散列); |^^^`std::collections::HashMap`无法使用默认格式化程序格式化 | 有没有这样的说法: use std::collections::HashMap; fn main() { let mut hash = HashMap::new();

将无法编译:

错误[E0277]:`std::collections::HashMap`未实现`std::fmt::Display'`
-->src/main.rs:6:20
|
6 | println!(“{}”,散列);
|^^^`std::collections::HashMap`无法使用默认格式化程序格式化
|
有没有这样的说法:

use std::collections::HashMap;

fn main() {
    let mut hash = HashMap::new();
    hash.insert("Daniel", "798-1364");
    println!("{}", hash);
}
并将其打印出来:

1)“丹尼尔”=>“798-1364”

您需要的是格式化程序:

println!("{}", hash.inspect());
应打印:

{“丹尼尔”:“798-1364”}
另见:

    • Rust 1.32引入了宏:

      这将打印:

      [src/main.rs:6]散列={
      “丹尼尔”:“798-1364”
      }
      

      对于自定义的严格类型,您需要使用#派生(调试)特性来修饰它们。
      use std::collections::HashMap;
      
      fn main() {
          let mut hash = HashMap::new();
          hash.insert("Daniel", "798-1364");
          println!("{:?}", hash);
      }
      
      use std::collections::HashMap;
      
      fn main() {
          let mut hash = HashMap::new();
          hash.insert("Daniel", "798-1364");
          dbg!(hash);
      }