rust find type
To find the type of a variable or expression in Rust, you can use the std::any::type_name
function. This function returns the type name as a &'static str
, which is a string that lives for the entire duration of the program. Here's an example of how to use it:
use std::any::type_name;
fn main() {
let my_variable = 42;
let my_type = type_name::<typeof(my_variable)>();
println!("Type of my_variable: {}", my_type);
}
In this example, we define a variable my_variable
of type i32
. We then use the type_name
function to get the type name of my_variable
and assign it to the my_type
variable. Finally, we print the type name using the println!
macro.
When you run this code, it will output the type name of my_variable
, which in this case will be i32
. You can replace my_variable
with any other variable or expression to get its type.