Introduction
In this article, we learn about the datatypes of Rust; there are two types of datatypes in Rust scaler and compound. Both us learn one by one. We will look at the various data types available in Rust, how to use them, and some best practices. We'll also review some typical Rust data processing, manipulation operations, and functions.
Rust has two main types of data types.
- Scalar Type
- Compound Type
Scalar Types in Rust
In this type of datatype, it returns only a single value. Rust has four scalar types Integers, Floating-point numbers, Booleans, and Characters. These types of data types are almost all programming languages. Let's go to know how itis work.
Integer Type in Rust
This type of datatype stores only numeric values without a fractional component. It is either a signed or unsigned integer; the meaning of unsigned integers is the value is positive, and the value of the signed integer may be positive or negative.
Tabel 1.1 Integer Types in Rust
Length |
Signed Integer |
Unsigned Integer |
8-bit |
i8 |
u8 |
16-bit |
i16 |
u16 |
32-bit |
i32 |
u32 |
64-bit |
i64 |
u64 |
128-bit |
i128 |
u128 |
arch |
isize |
usize |
In this table are three columns Length, Signed, and Unsigned integer type; length represents the total length of the integer datatype; here, i and you represent the datatype, either signed or unsigned. Now we understand how to calculate signed and unsigned integer types, so for signed integers that can store stores numbers from -(2^n-1) to (2^n-1)-1 and for unsigned integers 0 to (2^n)-1.
Example
fn main(){
let a =3; // i32
let b:i64 = 4; // i64
}
Explanation
In this article, we declare two variables, "a" and "b". In we declare direct assign value, the compiler takes default datatypes i32 for only integer, and in b variable, we declare i64 so that Rust takes i64 datatypes.
Floating Point Type
This data type stores only numeric values with fractional components or decimal points. Floating point types are f32 and f64; both are 32 and 64 bits sizes. All floating point types are signed.
Example
fn main(){
let a = 3.3 //f64
let b:f32 = 3.3 //f32
}
Explanation
In this article, we declare two variables, "a" and "b". We declare direct assign value so the compiler takes default datatypes f64 for only floating point, and in b variable, we declare f32 so that Rust takes f32 datatypes.
Boolean Type
This type of data type returns only two values, true or false, and that size is one byte in size. In Rust, the programming language is specified using bool.
Example
fn main(){
let a = true;
let b:bool = false; // here we use bool
}
Explanation
In this article, we declare two variables, "a" and "b" as the boolean type. So we have two methods. First, we declare the value directly in the variable, and the other is we use the bool data type.
Character Type in Rust
This article stores only alphabetic characters, like 'ABC' . It is used in only single quotes.
Example
fn main(){
let a = 'abc'; // here we are declare character
}
Explanation
In this example, we declare a variable a and assign a value "abc" in single quotes. Here we write anything in a single quote that means its value is character data types.
Compound Types in Rust
Compound types are those datatypes that store multiple values into one type. Rust programming language has two primitive compound types tuples and arrays.
Tuple Type
This compound datatype can store multiple values with different datatypes. It is fixed in size; we do not increase it at runtime of program execution. It is declared in parentheses and separated values with commas.
Example
fn main(){
let tup =(24,2.4,2);
let t:(i32, f64, u8) = (24,2.4, 2);
}
Explanation
In this example, we declare two variables, tup, and t. We directly assign a value to the first variable and the second variable. We assign a value with data types. We have both methods you can use according to your needs.
Array Type
This compound type can store multiple data with the same data type. It is declared in square brackets, and values are separated with a comma. It is fixed in Size; we can not increase size in runtime.
Example
fn main(){
let a = [1,2,3,4,5]; // here we declare array
}
Explanation
In this example, we declare a variable "a" and assign five values. The array's length is five, and find the length with the help of the len() function, which is the array's return length.
Conclusion
Rust is a very interesting programming language; it depends on your practice in learning Rust. In this article, we learn all types of Rust programming language datatype is one of the important features in all programming languages. All the best for the future.
FAQs
Q. What are the two main types of data types in Rust?
A. The two main data types in Rust are Scalar and Compound types.
Q. What are the four scalar types in Rust?
A. The four scalar types in Rust are Integers, Floating-point numbers, Booleans, and Characters.
Q. What is the difference between signed and unsigned integers in Rust?
A. Signed integers can store numbers from -(2^n-1) to (2^n-1)-1, while unsigned integers can store values from 0 to (2^n)-1.
Q. What is the size of the Boolean data type in Rust?
A. The size of the Boolean data type in Rust is one byte.
Q. What is a Tuple type in Rust?
A. A Tuple type in Rust is a compound data type that can store multiple values with different data types.
Q. What is an Array type in Rust?
A. An Array type in Rust is a compound data type that can store multiple values with the same data type.
Q. Can the size of a Tuple or Array be increased at runtime in Rust?
A.No, the size of a Tuple or Array in Rust is fixed and cannot be increased at runtime.