Rust for beginners — Part 4 — Data types

Manikandan SP
4 min readFeb 26, 2022

Let’s look into the different data types in rust

Note: Kindly have a look into the previous section of tutorial to understand about variables in rust

Previous section: https://medium.com/@manikandan96372/rust-for-beginners-part-3-variables-595f613bd91a

INTEGER

Integers in rust are of six types with respect to its length, they are 8-bit, 16-bit, 32-bit, 64-bit, 128-bit and arch. They could be both signed as well as unsigned integers i.e you can assign only positive numbers using unsigned integer, if you want to assign both positive as well as negative integers you must use signed integers.

i8, i16, i32, i64, i128, isize are the types for signed integers

u8, u16, u32, u64, u128, usize are the types for unsigned integers

if you assign the isize or usize type to a variable, it will automatically consider the architecture of your system (32-bit architecture or 64-bit architecture) and assign the respective bits to your variable.

fn main() {
let a: u8 = 1;
println!("{}", a);
}

On executing the above code you will get the following output,

1

Let’s try assigning different values to the above example

fn main() {
let a: u8 = 1000;
println!("{}", a);
}

But this time, on executing it would throw the following error

As the error message clearly signifies that it could accommodate numbers only till 255 as 8-bit unsigned integer type could hold only that, if you try to assign more you should probably increase variable bit size to u16 or u32 and so on.

FLOAT

Float types in rust are for handling decimal values. Float has only two types with it, which is f32 and f64. In float types you cannot have unsigned types, by default all of them are signed. If you don’t assign any type to float values by default it considers f64 as its type.

fn main() {
let a: f32 = 1.379;
println!("{}", a);
}

BOOLEAN

Boolean in rust are for handling true or false values which will be used in control flows of rust.

fn main() {
let a: bool = true;
}

CHARACTER

Character type in rust are used for handling alphabetical character, alphabets are enclosed using single quotes

fn main() {
let a = 'z';
println!("{}", a);
}

This would give you the following result

z

But what happens if you try to add more characters to it ?

fn main() {
let a = 'zqwerty';
println!("{}", a);
}

It would throw the following error

You could only assign one character at a time using char type.

TUPLES

Tuples in rust are used for grouping together different types of values. For example, if you have to group together an integer, a float value and a character, you could store and access them from one place which is tuples.

fn main() {
let a: (i8, f32, char) = (1, 2.765, 'a');
}

This is how you should create a tuple, mention the respective types and values, they must be enclosed within a parentheses.

But once you assign values to the tuple variable, you cannot add more values to it or delete an existing value from it, it is fixed !

You can access individual values from tuples using the period (.) along with its index

fn main() {
let a: (i8, f32, char) = (1, 2.765, 'a');
println!("{}", a.0);
}

This will give the following output

1

Index of tuples start from 0.

ARRAY

You can also group elements in rust using an array. But unlike tuples, array can contain values of same type and not different types. You should enclose the values inside a bracket []

fn main() {
let a = [1, 2, 3, 4, 5];
}

If you want to add types to your array and number of values it can accommodate, you should use the following syntax

fn main() {
let a: [i8; 5] = [1, 2, 3, 4, 5];
}

You can access individual elements from your array using their index

fn main() {
let a: [i8; 5] = [1, 2, 3, 4, 5];
println!("{}", a[0]);
}

Array index starts from 0.

Let’s discuss about Strings in the next section

--

--