use std::collections::HashMap;
fn main() {
let mut hash = HashMap::new();
hash.insert("Daniel", "798-1364");
println!("{}", hash);
}将无法编译:
error[E0277]: `std::collections::HashMap<&str, &str>` doesn't implement `std::fmt::Display`
--> src/main.rs:6:20
|
6 | println!("{}", hash);
| ^^^^ `std::collections::HashMap<&str, &str>` cannot be formatted with the default formatter
|有没有办法说这样的话:
println!("{}", hash.inspect());并将其打印出来:
1) "Daniel" => "798-1364"发布于 2016-08-19 21:00:20
您要查找的是Debug格式化程序:
use std::collections::HashMap;
fn main() {
let mut hash = HashMap::new();
hash.insert("Daniel", "798-1364");
println!("{:?}", hash);
}这应该打印出来:
{"Daniel": "798-1364"}另请参阅:
发布于 2019-01-23 16:01:37
Rust 1.32引入了dbg宏:
use std::collections::HashMap;
fn main() {
let mut hash = HashMap::new();
hash.insert("Daniel", "798-1364");
dbg!(hash);
}这将打印以下内容:
[src/main.rs:6] hash = {
"Daniel": "798-1364"
}
https://stackoverflow.com/questions/39039700
复制相似问题