#![feature(core_intrinsics)] fn print_type_of<T>(_: T) { println!("{}", std::intrinsics::type_name::<T>() ); } fn main() { print_type_of(3); // prints "i32" print_type_of(32.90); // prints "f64" print_type_of(vec![102, 111, 111]); // prints "alloc::vec::Vec<i32>" print_type_of(b"foo12".to_vec()); // prints "alloc::vec::Vec<u8>" print_type_of(b"foo12".to_owned()); // prints "[u8; 5]" print_type_of( "bar".as_bytes().to_vec()); // prints "alloc::vec::Vec<u8>" print_type_of( "bar".as_bytes().to_owned()); // prints "alloc::vec::Vec<u8>" println!("==================================================="); print_type_of("foo"); // prints "&str" let s = "bar"; print_type_of(&s); // prints "&&str" print_type_of(*&s); // prints "&str" print_type_of(&&s); // prints "&&&str" print_type_of([0x08, 0x09, 0x0a, 0x0b, ]); // prints "[i32; 4]" print_type_of([0x08, 0x09, 0x0a, 0x0b, 0x0b,]); // prints "[i32; 5]" print_type_of("foo".as_bytes()); // prints "&[u8]" print_type_of(b"foo15"); // prints "&[u8; 5]" print_type_of("foo".to_string()); // prints "alloc::string::String" print_type_of("foo".to_owned()); // prints "alloc::string::String" print_type_of(String::from("foo")); // prints "alloc::string::String" print_type_of(String::from_utf8(vec![102, 111, 111]).unwrap()); //prints "alloc::string::String" println!("{}",String::from_utf8(vec![102, 111, 111]).unwrap()); //prints "foo" print_type_of(std::str::from_utf8(&[0x66, 0x6F, 0x6F,]).unwrap()); //prints "&str" println!("{}",std::str::from_utf8(&[0x66, 0x6F, 0x6F,]).unwrap()); //prints "foo" println!("{}",std::str::from_utf8(&[102, 111, 111,]).unwrap()); //prints "foo" println!("{}",std::str::from_utf8( "foo".as_bytes()).unwrap()); //prints "foo" println!("{}",std::str::from_utf8(&vec![102, 111, 111]).unwrap()); //prints "foo" print_type_of(vec![102, 111, 111].as_slice()); //prints "&[i32]" println!("{:?}",vec![102, 111, 111].as_slice()); //prints "[102, 111, 111]" }
需要切换rustup到nightly版本才能运行cargo run
如何切换 可参考上一篇文章
参考:https://www.cnblogs.com/chen8840/p/12698527.html
https://zhuanlan.zhihu.com/p/372082802
https://blog.csdn.net/wowotuo/article/details/83927644
https://cloud.tencent.com/developer/ask/66463