Skip to main content
eLearner.app
Module 1 · Lesson 2 of 22/14 in the course~10 min
Module lessons (2/2)

Primitive Types

Rust is a statically typed language, which means it must know the type of all variables at compile time. Usually, the compiler can infer the type based on the assigned value, but sometimes it is necessary or helpful to specify it manually.

Primitive types in Rust are divided into two main categories: scalars (single value) and compounds (group of values).

Scalar Types

Scalar types represent a single value. Rust has four fundamental scalar types:

  1. Integers: Whole numbers. They can be signed (i8, i16, i32, i64, i128, isize) or unsigned (u8, u16, u32, u64, u128, usize). The default type is i32.
  2. Floating-point: Numbers with decimal points. They can be f32 or f64 (double precision, default).
  3. Booleans: Represent logical truth with true or false (type bool).
  4. Characters: A single Unicode scalar value enclosed in single quotes (type char), e.g., 'a', 'ℤ', or emoji '😻'.

Example of explicit type annotation:

Code
let x: i64 = 42;
let y: f64 = 3.1415;
let is_rust_awesome: bool = true;
let heart_emoji: char = '❤';

Compound Types

Compound types can group multiple values into a single type. Rust has two main primitive compound types:

Tuples

A tuple groups values of different types into a fixed-size collection:

Code
let person: (String, i32) = (String::from("Alice"), 30);

// Destructuring to extract values
let (name, age) = person;

// Direct access with index using dot (.)
let name_direct = person.0;
let age_direct = person.1;

Arrays (Fixed-size lists)

An array groups multiple elements of the same type into a fixed-size collection stored on the stack:

Code
let numbers = [1, 2, 3, 4, 5]; // Inferred as [i32; 5]
let first = numbers[0]; // Access by index
let third = numbers[2];

If you try to access an index outside the array boundaries (e.g., numbers[10]), Rust will immediately crash (panic) at runtime instead of allowing unauthorized memory access.

Comparison

FeatureTupleArray
Element typesCan be differentMust be the same
SizeFixedFixed
AccessIndex via dot (e.g., .0)Square brackets (e.g., [0])

Try it yourself

Exercise#rust.m1.l2.e1
Attempts: 0Loading…

Declare a tuple named stats containing the age 30 (integer) and the score 95.5 (float). Next, print the score to the screen by directly accessing the second element of the tuple using its index.

Loading editor…
Show hint

Create the tuple with `let stats = (30, 95.5);`. Access the second element using `stats.1` inside the `println!` macro.

Solution available after 3 attempts

Exercise#rust.m1.l2.e2
Attempts: 0Loading…

Declare an array named numbers containing the integer values from 1 to 5. Print the third element of the array (index 2) to the screen using println!.

Loading editor…
Show hint

Declare the array with `let numbers = [1, 2, 3, 4, 5];` and access the third element by indexing it with `[2]`.

Solution available after 3 attempts

Exercise#rust.m1.l2.e3
Attempts: 0Loading…

Given the tuple `let point = (10, 20);`, use destructuring to extract the values into two variables named `x` and `y`. Then, print the sum of `x` and `y`.

Loading editor…
Show hint

Use the syntax `let (x, y) = point;` to destructure the tuple and then sum the variables in `println!`.

Solution available after 3 attempts