How do I print the type of a variable in Rust?

后端 未结 11 2145
无人共我
无人共我 2020-11-22 08:49

I have the following:

let mut my_number = 32.90;

How do I print the type of my_number?

Using type and

11条回答
  •  悲哀的现实
    2020-11-22 09:23

    ** UPDATE ** This has not been verified to work any time recently.

    I put together a little crate to do this based off vbo's answer. It gives you a macro to return or print out the type.

    Put this in your Cargo.toml file:

    [dependencies]
    t_bang = "0.1.2"
    

    Then you can use it like so:

    #[macro_use] extern crate t_bang;
    use t_bang::*;
    
    fn main() {
      let x = 5;
      let x_type = t!(x);
      println!("{:?}", x_type);  // prints out: "i32"
      pt!(x);                    // prints out: "i32"
      pt!(5);                    // prints out: "i32"
    }
    

提交回复
热议问题