How do I print variables in Rust and have it show everything about that variable, like Ruby's .inspect?

情到浓时终转凉″ 提交于 2019-12-04 03:00:30
Nate Mara

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:

halfelf

Rust 1.32 introduced the dbg macro:

use std::collections::HashMap;

fn main() {
    let mut hash = HashMap::new();
    hash.insert("Daniel", "798-1364");
    dbg!(hash);
}

This will print:

[src/main.rs:6] hash = {
    "Daniel": "798-1364"
}

🎉

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!