how to find the data type of a number and print
In Rust, the type of every expression is known at compile-time. If you use an IDE or editor plug-in like rust-analyzer then your editor can display the types for you.
Integer literals like 5
can be of any integral type. The type of the literal expression will be inferred from context. If the context doesn't constrain the type at all, the default is i32
. The default type for a floating-point literal like 5.0
is f64
.
You can assign an expression to a variable and provide a type annotation if you want to control the type. This will also print an error message if the expression is of some other type:
let x: i64 = my_value; // compiler error if `my_value` has a different type
On nightly, you can also use type_name_of_val
to print the type of a value at run-time (with some limitations).
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.