Rust实现打印变量的的类型名
use std::fmt::{Debug};
#[derive(Debug)]
enum Unit{
P1,P2
}
//实行trait bound
fn type_v<T>(t:T) where T:Debug {
println!("{:?},{}",t,core::any::type_name::<T>());
}
fn main() {
let ref r1 = 3;
let r2 = &3;
let r3 = Unit::P1;
type_v(&mut (1.0 as f64));
type_v(r1);
type_v(r2);
type_v(r3);
}
输出
1.0,&mut f64
3,&i32
3,&i32
P1,fib::Unit