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);
}