RUST: Numeric Data Types

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:

Length Signed Unsigned
8-bit i8 u8
16-bit i16 u16
32-bit i32 u32
64-bit i64 u64
CPU Arch (32/64 bits) isize usize

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.


knoldus-advt-sticker


Written by 

Sourabh Verma is a Software Consultant with experience of more than 2 years. He has a deep understanding of Java and familiar with Spring Framework, JPA, Hibernate, JavaScript, Spark, Scala, AngularJS, Angular 4. He is an amazing team player with self-learning skills and a self-motivated professional. He loves to play with Real-time problems, Big data, Cloud computing, Agile Methodology and Open Source Technology. He is eager to learn new technologies and loves to write blogs and explore nature.

Leave a Reply

%d bloggers like this: