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

ghz 7months ago ⋅ 65 views

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?

Answers

In Rust, you can use the std::any::type_name function to get the name of the type of a variable. However, this function requires the variable to be a reference. Here's how you can print the type of my_number:

let my_number = 32.90;
println!("Type of my_number: {}", std::any::type_name::<typeof(&my_number)>());

This code snippet will print the type of my_number, which in this case would be f64.