4 回答
TA贡献1815条经验 获得超12个赞
let () = x;
error[E0308]: mismatched types --> <anon>:2:29 | 2 | let mut my_number: () = 32.90; | ^^^^^ expected (), found floating-point variable | = note: expected type `()` = note: found type `{float}` error: aborting due to previous error
error: no method named `what_is_this` found for type `{float}` in the current scope --> <anon>:3:15 | 3 | my_number.what_is_this(); | ^^^^^^^^^^^^ error: aborting due to previous error
error: attempted access of field `what_is_this` on type `{float}`, but no field with that name was found --> <anon>:3:5 | 3 | my_number.what_is_this | ^^^^^^^^^^^^^^^^^^^^^^ error: aborting due to previous error
{float}
f32
f64
{float}
f64
i32
.)
f32
f64
32.90.eq(&32.90)
f64
TA贡献1805条经验 获得超9个赞
std::intrinsics::type_name
#![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"}
TA贡献2051条经验 获得超10个赞
type_of
trait TypeInfo { fn type_of(&self) -> &'static str;}impl TypeInfo for i32 { fn type_of(&self) -> &'static str { "i32" }}impl TypeInfo for i64 { fn type_of(&self) -> &'static str { "i64" }}//...
trait TypeInfo { fn type_name() -> String; fn type_of(&self) -> String;}macro_rules! impl_type_info { ($($name:ident$(<$($T:ident),+>)*),*) => { $(impl_type_info_single!($name$(<$($T),*>)*);)* };}macro_rules! mut_if { ($name:ident = $value:expr, $($any:expr)+) => (let mut $name = $value;); ($name:ident = $value:expr,) => (let $name = $value;);}macro_rules! impl_type_info_single { ($name:ident$(<$($T:ident),+>)*) => { impl$(<$($T: TypeInfo),*>)* TypeInfo for $name$(<$($T),*>)* { fn type_name() -> String { mut_if!(res = String::from(stringify!($name)), $($($T)*)*); $( res.push('<'); $( res.push_str(&$T::type_name()); res.push(','); )* res.pop(); res.push('>'); )* res } fn type_of(&self) -> String { $name$(::<$($T),*>)*::type_name() } } }}impl<'a, T: TypeInfo + ?Sized> TypeInfo for &'a T { fn type_name() -> String { let mut res = String::from("&"); res.push_str(&T::type_name()); res } fn type_of(&self) -> String { <&T>::type_name() }}impl<'a, T: TypeInfo + ?Sized> TypeInfo for &'a mut T { fn type_name() -> String { let mut res = String::from("&mut "); res.push_str(&T::type_name()); res } fn type_of(&self) -> String { <&mut T>::type_name() }}macro_rules! type_of { ($x:expr) => { (&$x).type_of() };}
impl_type_info!(i32, i64, f32, f64, str, String, Vec<T>, Result<T,S>)fn main() { println!("{}", type_of!(1)); println!("{}", type_of!(&1)); println!("{}", type_of!(&&1)); println!("{}", type_of!(&mut 1)); println!("{}", type_of!(&&mut 1)); println!("{}", type_of!(&mut &1)); println!("{}", type_of!(1.0)); println!("{}", type_of!("abc")); println!("{}", type_of!(&"abc")); println!("{}", type_of!(String::from("abc"))); println!("{}", type_of!(vec![1,2,3])); println!("{}", <Result<String,i64>>::type_name()); println!("{}", <&i32>::type_name()); println!("{}", <&str>::type_name());}
i32 &i32 &&i32 &mut i32 &&mut i32 &mut &i32 f64 &str &&str String Vec<i32> Result<String,i64> &i32 &str
TA贡献1772条经验 获得超8个赞
UPD
std::intrinsics::get_tydesc<T>()
fn print_type_of<T>(_: &T) -> () { let type_name = unsafe { (*std::intrinsics::get_tydesc::<T>()).name }; println!("{}", type_name);}fn main() -> () { let mut my_number = 32.90; print_type_of(&my_number); // prints "f64" print_type_of(&(vec!(1, 2, 4))); // prints "collections::vec::Vec<int>"}
{:?}
添加回答
举报