Scalar Datatypes in Rust
All initialized variables use data-type during declaration to restrict the type of data to be stored. Therefore, we can say that data types are used to tell the variables the type of data they can store.
In this article, we will specifically talk about the Scalar Data types in the Rust programming language. A Scalar type in Rust represents a single value. For example, an int data type only stores single integer data and not multiple integers nor a combination of different data types.
Rust has four primary scalar types:
- Floating-point numbers
Let’s discuss them in detail.
Integers are everywhere, Integer is a whole number with a sign and without a fractional part. For example 1, 5, -9, 200001, -15489752are integers
Integers are one of the primitive data types in Rust, Rust compiler can automatically detect Integers and also the programmer can explicitly define integer type.
6 9 -7
In Rust, integer declaration indicates that the value it’s associated with should be an unsigned integer that starts with u (or) signed integer types start with i and takes up 32 bits of space with a minimum of 8 bits.
The signed complement integers types have:
|i8||-(27)||27 – 1|
|i16||-(215)||215 – 1|
|i32||-(231)||231 – 1|
|i64||-(263)||263 – 1|
|i128||-(2127)||2127 – 1|
Floating-point numbers integers with the decimal parts. For example 3.22, -5.89, 200.0000. Rust also has a primitive datatype for floating numbers. Rust’s floating-point types are either f32 and f64, which are 32 bits and 64 bits in size, respectively. Like Integers, you can both explicitly and implicitly define floating-point numbers. Syntax of floating-point numbers
200 9.894 -7.4
Boolean type is one of the built-in data types provided by Rust, which are defined by the True or False keywords. Generally, it is used to represent the truth values of the expressions. Boolean types in Rust are provided with 1-bit memory. The Boolean type in Rust is specified using the bool keyword.
The character data type is used for storing characters. The keyword used for the character data type is char. Characters typically require 1 byte of memory space and range from -128 to 127 or 0 to 255.
Simple Rust program with all scalar datatypes:
9 2.9 14122020 true a ????