如何在Rust中打印变量的类型?

#![feature(core_intrinsics)]
fn print_type_of<T>(_: T) {
    println!("{}", unsafe { std::intrinsics::type_name::<T>() });
}

fn main() {
    print_type_of(32.90);          // prints "f64"
    print_type_of(vec![1, 2, 4]);  // prints "std::vec::Vec<i32>"
    print_type_of("foo");          // prints "&str"
}

需要切换rustup到nightly版本才能运行cargo run

查看rustup版本

rustup toolchain list

切换到nightly版本

rustup default nightly

 

posted on 2020-04-14 15:54  chen8840  阅读(1817)  评论(0编辑  收藏  举报