I have the following:
let mut my_number = 32.90;
How do I print the type of my_number
?
Using type
and type_of
did not work. Is there another way I can print the number's type?
I have the following:
let mut my_number = 32.90;
How do I print the type of my_number
?
Using type
and type_of
did not work. Is there another way I can print the number's type?
Some other answers don't work, but I find that the typename crate works.
Create a new project:
Modify the Cargo.toml
Modify your source code
The output is:
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. A little dialog should come up with information about the variable including the type.
If you know all the types beforehand, you can use traits to add a
type_of
method:No intrisics or nothin', so although more limited
this is the only solution here that gets you a string and is stable.(see French Boiethios's answer) However, it's very laborious and doesn't account for type parameters, so we could...Let's use it:
output:
Rust Playground
You can also use the simple approach of using the variable in
println!("{:?}", var)
. IfDebug
is not implemented for the type, you can see the type in the compiler's error message:(playpen)
It's dirty but it works.