Print type of variable
-
type_name
-
type_name_of_val
-
Sometimes during development, during the exploration of Rust it is useful to print the type of a varible.
-
This is one way to do it.
-
It will be added to the stanard library as type_name_of_val.
fn main() { let an_integer = 42; print_type(&an_integer); let a_float = 4.2; print_type(&a_float); let an_str = "Hello"; print_type(&an_str); let a_string = String::from("World"); print_type(&a_string); let a_vector = vec![3, 4, 5]; print_type(&a_vector); // An iterator let readdir = std::path::Path::new(".").read_dir().unwrap(); print_type(&readdir); } fn print_type<T>(_: &T) { println!("{:?}", std::any::type_name::<T>()); }
"i32"
"f64"
"&str"
"alloc::string::String"
"alloc::vec::Vec<i32>"
"std::fs::ReadDir"