DEV Community

Cover image for Primitive types and data types in Rust
Bekka
Bekka

Posted on

Primitive types and data types in Rust

Every programming language has Its basic units and types, the way human languages are made up of vowels and consonants. They make up every line of your code in your program.
In this post, we are going to learn about Rust's data types.

What are types in Rust?
Rust is a statically typed language, which means that it must know the types of all variables at compile time. The compiler can actually infer what type we want to use based on the value we provided and also how we use it. Rust has two data types subsets. They are also referred to as Rust's primitive types. They are:

  • Scalar types

  • Compound types

Scalar types

Scalar types represent a single value. Rust has four primary types: Integers, floating-point numbers, booleans, characters.

  • Integers:- This is a number without a fractional component. They are used to represent whole numbers e.g 2, 3, 5 e.t.c They are also referred to as "int". In Rust, There are different values to declare the type of an int value. Examples are 8-bit -> u8, 16-bit -> u16. They could be signed like this
let negative_two: i8 = -2;
Enter fullscreen mode Exit fullscreen mode

or unsigned like this

let two: u8 = 2;
Enter fullscreen mode Exit fullscreen mode

The "i" means it is signed, (i.e it could be negative or positive) while the "u" means it is unsigned (i.e it is always positive). Numbers are represented in bits size, I recommend learning more about "bits" if you are unfamiliar with them.

  • Floating point numbers:- Rust's floating-point types are f32 and f64, which are 32 bits and 64 bits respectively. These are also known as "decimal numbers" in the math world 😄. Of course, you can perform basic numeric operations on numbers (int) and floating-point number but note that you can't do numeric operations between an i32 number and an f32. You have to convert i32 to f32 for such operations. Let's give a try
let decimal_num: f32 = 8.4;
    let whole_num: i32 = 8;

    println!("The answer is {}", decimal_num + whole_num);

Enter fullscreen mode Exit fullscreen mode

This will compile to an error, telling you "cannot add i32 to f32". i32 is treated as a different type from f32 although to us humans they are still numbers. In machine code they are different. Let's fix the error in our code.

let decimal_num: f32 = 8.4; //a floating-point number
    let whole_num: i32 = 8;

    println!("The answer is {}", decimal_num + whole_num as f32);

Enter fullscreen mode Exit fullscreen mode

This compiles and the answer is 16.4.

  • The boolean type:- Just like in most programming languages, a boolean has two possible values: true or false. This is one byte in size. It is specified using the keyword "bool"

  • The character type:- Rust's char type is the most primitive type in the language, according to the Rust book. They are specified in single quotes not double quotes. They are four bytes in size and the indexing patterns are not usually what you think it is.

let single_char: char = 'c'; //a char type
let single_string: &str = "c"; //a string slice

Enter fullscreen mode Exit fullscreen mode

Compound types

They can group multiple values into one type, They are two in this category.

  1. Tuple: It is a general way of grouping together a number of of values with a variety of types into one compound type. They have a fixed length, they cannot grow or shrink in size. This is an example of a tuple below.
  let two_values = (1, true);
    println!("pair is {:?}", two_values);
Enter fullscreen mode Exit fullscreen mode
  1. Arrays: Unlike tuples, an array can only have one type, i.e all values of an array must have just one type. They have a fixed length, you can access them using indexes. If you want a growable list you can use a Vector instead. Vectors are stored on the heap while arrays are stored on the stack.
let array_one = [2, 3, 5, 7]; //without a type, rust can infer the type of this array

let array_two: [i32; 4] = [2, 3, 5, 7]; //this is the standard way for initialising an array
println!("array {:?}", array_one);

Enter fullscreen mode Exit fullscreen mode

Generic types

This is not a type but can be likened to a tool that allows to write code for multiple contexts with different types. This might seem complex at first, but it is a great tool for grouping code together.

I hope this cleared out some confusions on types on Rust, as they are the bare foundations to the Rust programming language. Remember to checkout the "Rust book", It's a great book to start learning how to code in Rust.

Top comments (0)