- Views: 1
- Report Article
- Articles
- Writing
- Article Marketing
Data types in Rust
Posted: Oct 21, 2020
Every value in Rust is of a certain data type.
Rust automatically infers primitive data type without explaining it explicitly.
Rust is a statically typed language, which means that it must know the types of all variables at compile time.
Data types in RUST:
Scalar type
A scalar type represents a single value. Rust has four primary scalar types.
- Integer.
- Floating-point.
- Boolean.
- Character
Compound type
Compound type can group multiple values into one type. Rust has two primitive compound types.
- Tuples
- Arrays
Scalar type
Integer type
Length Signed Unsigned
8-bit i8 u8
16-bit i16 u16
32-bit i32 u32
64-bit i64 u64
Arch isize usize
In Rust each integer can be either signed or unsigned and has an explicit size.
The isize and usize types depend on the kind of computer your program is running on: 64 bits if you’re on a 64-bit architecture and 32 bits if you’re on a 32-bit architecture.
If we do not declare type of an integer then Rust will by default take i32 for an integer.
Each signed variant can store numbers from -(2n - 1) to 2n - 1 - 1 inclusive.
Unsigned variants can store numbers from 0 to 2n - 1.let variable: u32 = 100;
We can write integer literals in any of the forms given below
Number literals Example
Decimal 98_222
Hex 0xff
Octal 0o77
Binary 0b1111_0000
Byte (u8 only) b’A’
2. Floating-Point Types:
Rust also has two primitive types for floating-point numbers, which are numbers with decimal points.
Rust’s floating-point types are f32 and f64, which are 32 bits and 64 bits in size, respectively.
If we do not declare type of floating point then Rust will by default take f64 for that floating point type.
The f32 type is a single-precision float, and f64 has double precision.
let variable: f32 = 1.23;
3. Boolean Type:
A Boolean type in Rust has two possible values: true and false.
Booleans are one byte in size.
The Boolean type in Rust is specified using bool.
let variable: bool = true;
4. Character Type:
Rust supports letters too.
Rust’s char type is the language’s most primitive alphabetic type.
Characters are specified with single quotes, as opposed to string literals, which use double quotes.
Rust’s char type is four bytes in size and represents a Unicode Scalar Value.
let c = ‘z’;
Compound Type
1. Tuple Type:
A tuple is a general way of grouping together some number of other values with a variety of types.
Tuples have a fixed length, once declared; they cannot grow or shrink in size.
Contain heterogeneous data.
We create a tuple by writing a comma-separated list of values inside parentheses.
We can access a tuple element directly by using a period (.) followed by the index of the value we want to access.
2. Array Type:
Another way to have a collection of multiple values is with an array.
Contains homogenous data.
Every element of an array must have the same type not like a Tuple.
Writing an array's type is done with square brackets containing the type of each element in the array followed by a semicolon and the number of elements in the array.
We can access elements of an array using indexing in square bracket.
An array isn’t flexible and not allowed to grow or shrink in size.
declaration: let array = [1, 2, 3, 4, 5];
Accessing an array element: let value1 = array[0];
Example
fn main() {
let a = [1, 2, 3, 4, 5];
let element = 3;
println!("The value of
element is: {}", a[element]);
}
Result: The value of element is: 4
Mansoor Ahmed Chemical Engineer,Web developer