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

前端 未结 2 1232
挽巷
挽巷 2021-02-13 06:02
use std::collections::HashMap;

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

相关标签:
2条回答
  • 2021-02-13 06:22

    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:

    • What is the difference between println's format styles?
    0 讨论(0)
  • 2021-02-13 06:41

    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"
    }
    

    0 讨论(0)
提交回复
热议问题