我有以下情况:let mut my_number = 32.90;如何打印my_number?使用type和type_of不起作用。还有别的方法可以打印号码的字体吗?
4 回答
精慕HU
TA贡献1845条经验 获得超8个赞
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贡献2012条经验 获得超12个赞
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"}
慕尼黑5688855
TA贡献1848条经验 获得超2个赞
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>"}
{:?}
添加回答
举报
0/150
提交
取消