use std::collections::HashMap;
fn main() {
let mut hash = HashMap::new();
hash.insert("Daniel", "798-1364");
println!("{}", hash);
}
will fail to compile:
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
|
Is there a way to say something like:
println!("{}", hash.inspect());
and have it print out:
1) "Daniel" => "798-1364"
You first include the character f before the opening and closing quotation marks, inside the print() function. To print a variable with a string in one line, you again include the character f in the same place – right before the quotation marks.
If your just wanting to know the type of your variable during interactive development, I would highly recommend using rls (rust language server) inside of your editor or ide. You can then simply permanently enable or toggle the hover ability and just put your cursor over the variable.
What you're looking for is the Debug
formatter:
use std::collections::HashMap;
fn main() {
let mut hash = HashMap::new();
hash.insert("Daniel", "798-1364");
println!("{:?}", hash);
}
This should print:
{"Daniel": "798-1364"}
See also:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With