可以试试std::any::type_name
。注意,这个是unstable的。
use std::collections::HashSet;
fn print_type_of<T>(_: &T) {
println!("{}", std::any::type_name::<T>())
}
fn main() {
let mut s = HashSet::new();
let ve = vec![1, 2, 1, 3, 2, 4];
print_type_of(&s);
for v in &ve {
println!("{}", s.insert(v));
}
}
std::collections::hash::set::HashSet<&i32>
true
true
false
true
false
true
参考文献:
如何在Rust中打印一个变量的类型?
https://stackoverflow.com/questions/21747136/how-do-i-print-the-type-of-a-variable-in-rust/43508373#43508373
https://doc.rust-lang.org/stable/std/any/fn.type_name.html