rust-打印变量类型
使用type_name
通过使用std::any::type_name
获得.
编写函数
fn print_type_of<T>(_: &T) {
println!("The type is: {}", type_name::<T>());
}
Example
use std::any::type_name;
fn print_type_of<T>(_: &T) {
println!("The type is: {}", type_name::<T>());
}
fn main() {
let a = String::from("Gate");
let ref b = a;
let ref c = b;
print_type_of(&a);
print_type_of(&b);
print_type_of(&c);
}