You must be wondering what is so special about numeric data types in RUST, that we are focusing on them so much, definitely, you will get to know after finishing this blog.
Generally, other programming languages follow short, int, float, long, double etc as numeric data types as per their storage size and capacity while in RUST numeric data type are like :
i[size_in_bits] or u[size_in_bits]
Here, i or u stands for signed and unsigned numbers respectively.
Size in bits (in multiple of 8bits) means the storage size and minimum/maximum numeric value it can hold.
Note: By default, i32 is the integer data type, in case you don’t specify.
If you don’t have any idea how to calculate the size in bits to specify, you can calculate using the following formulae for the signed number where n is no of bits:
-(2n – 1) to 2n – 1 – 1 -> which equals -128 to 127 for n = 8
For unsigned it will be:
0 to 2n – 1 -> which equals 0 to 255 for n = 8
Below Tables show the different integer numeric data types in RUST:
|CPU Arch (32/64 bits)||
We haven’t mentioned anything about decimals/real numbers, as above all mentioned data type will only hold the integer value. So to deal with floating values, RUST provides us data type in the same above-mentioned manner.
Rust’s floating-point types are f32 and f64, which are 32 bits and 64 bits in size, respectively. f32 type is a single-precision float and f64 has double precision.
Note: By default, f64 is the floating data type, in case if you don’t specify as in current system all are 64 bits CPU, as it hardly make any difference in performance and it is capable of double precision as well.
So, by now you must have understood what’s the big difference in RUST while dealing with numeric data type whether it’s integer or floating.