DEV Community

Cover image for Part-4: Data Types in Rust
Bhavesh Yadav
Bhavesh Yadav

Posted on • Edited on

Part-4: Data Types in Rust

Hey folks, welcome to part-4 of my blog series on rust. In this series we are learning rust programming language from scratch, if you haven't read the previous parts then i highly recommend to read those first so that you get the context of what we are doing here. Read here.

So without wasting any time lets get started!


Every variable in Rust is of a certain data type, which basically tells Rust what kind of data is being specifit so it knows how to proceed with that data. We'll majorly see two data type subsets: scalar and compound.

Rust is a statically typed language so it should know the type of each variable at the compile time.

If you're already familiar with TypeScript then rust works something like that only. For example look at the following example:

let guess: u32 = "42".parse().expect("Not a number!");
Enter fullscreen mode Exit fullscreen mode

Try pasting the above code in your text editor and then try to print it out like following:

println!("The value of guess is {guess}");
Enter fullscreen mode Exit fullscreen mode

Voila! Everything works fine, but now try to remove the : u32 and then try to print it, you'll get an error, because you have to tell rust what do you expect.

So basically what the above code is doing is, there is a variable called guess and which is initially a string and we are trying to parse it (basically converting it into a number) and then we add a expect function, which basically expects the parsed value to be a u32 and if it is not a u32 then it will throw a error called Not a number!.


What is a Scalar Type?

A scalar type represents a single value. In rust there are four primary scalar types i.e. integers, floating-point numbers, Booleans and characters. If you're familiar with coding then you may recognise these are same in almost all programming languages.

Now lets see how they work in rust:

All the below data and images are directly from the rust programming language book. You can check this book out here.


1. Integer

An integer is a number without a fractional component. The type we used in the above example was of unsigned integer that takes up 32 bits of size (signed integer types start with i and unsigned start with u).

You can check the following table to know more about the different types of integers available in rust.

Image description

Signed basically means that integer can be negative as well, unsigned integers are always positive and therefore it can be written unsigned.

You can check more on integer types in the official rust programming language book here.


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. The default type is f64 because on modern CPUs, it’s roughly the same speed as f32 but is capable of more precision. All floating-point types are signed.

Rust also supports floating point numbers which are basically numbers with decimal points. There are only two types of floating point types in rust i.e. f32 and f64, which ig you'll guess at this point f32 is a 32 bit and f64 is 64 bit in size.

The default type is f64 in rust because on modern CPUs its roughly same speed as f32 but it is capable of more precision. All floating-point types are signed.

Numeric Operations

Rust supports all basic mathematic operations you'd expect for all number types.

fn main() {
    let sum = 5 + 10;

    let difference = 95.5 - 4.3;

    let product = 4 * 30;

    let quotient = 56.7 / 32.2;

    let truncated = -5 / 3;

    let remainder = 43 % 5;
}
Enter fullscreen mode Exit fullscreen mode

3. Boolean Type

A boolean type in rust, just like in almost all programming languages, can have two values either true or false.

You can write a boolean as follows in rust:

let ironman = true;

let superman: bool = false; // Just with explicit type annotation
Enter fullscreen mode Exit fullscreen mode

4. Character Type

Here are some examples of rust's character types.

let x = 'x';
let y: char = 'X'; // with explicit type annotation
let laugh = '😂';
Enter fullscreen mode Exit fullscreen mode

Note there here the values are inside single quotes unlike other strings.

Read more about character type here.


What is a Compound Type?

A compound type just groups multiple single scalar types. Rust have two compound types as primitives i.e. arrays and tuples.

1. Tuples

A tuple is a general way to group a number of scalar types into one single compound type.

We can create a tuple by writing a comma-separated list of values inside parentheses. Each position has its type and order matters in tuples, and also different values need not to have same type.

A simple example of a tuple is mentioned below:

let some_tup: (u32, f64, u8) = (500, 2.543, 1);
Enter fullscreen mode Exit fullscreen mode

You can also extract values from tuple like following:

fn main() {
   let some_tup: (u32, f64, u8) = (500, 2.543, 1);

   let (a, b, c) = some_tup;

   println!("The value of b is {b}");
}
Enter fullscreen mode Exit fullscreen mode

The above code first creates a tuple with some values of different types and then extracts or destructure the value from that tuple and then finally print a variable.

We can also extract a particular element directly using a . and then followed by its index(index starts from 0). You can do this like following:

For simplicity i've removed the main function, you need to wrap this with main function in order for this to work.

let some_tup: (u32, f64, u8) = (500, 2.543, 1);
let first = some_tup.0;
let third = some_tup.2;

println!("The value of first is {} and value of third is {}", first, third);
Enter fullscreen mode Exit fullscreen mode

Learn more about tuples here.


2. Array Type

Another way to grouping multiple values together is an array. But there is one major difference between these two i.e. each element in a array must be of same type. Arrays are useful when you know the number of elements inside it won't change like days in a week or months in a year.

An array is declared like following:

let arr = [1,2,3,4,5];
Enter fullscreen mode Exit fullscreen mode

Arrays are also very useful when you want the data to be stored in the stack rather than heap. We'll learn more about stack and heap in upcoming blogs, trust me its a very interesting topic.

You can access a particular element like following:

let arr = [1,2,3,4,5];

let first = arr[0];

let last = arr[4];

println!("The value of first is {first} and value of last is {last}");
Enter fullscreen mode Exit fullscreen mode

Thats it!

Whew, that were so many data types for someone new in programming, but these are one of the most important topics for any programming language.

In the upcoming blogs, we'll see functions, comments and control flow in rust. Keep waiting for next one, Till then!

Happy Coding!


You can find me on twitter here.


Top comments (0)