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:
- Integers: Whole numbers. They can be signed (
i8,i16,i32,i64,i128,isize) or unsigned (u8,u16,u32,u64,u128,usize). The default type isi32. - Floating-point: Numbers with decimal points. They can be
f32orf64(double precision, default). - Booleans: Represent logical truth with
trueorfalse(typebool). - Characters: A single Unicode scalar value enclosed in single quotes (type
char), e.g.,'a','ℤ', or emoji'😻'.
Example of explicit type annotation:
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:
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:
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
| Feature | Tuple | Array |
|---|---|---|
| Element types | Can be different | Must be the same |
| Size | Fixed | Fixed |
| Access | Index via dot (e.g., .0) | Square brackets (e.g., [0]) |
Try it yourself
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.
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
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!.
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
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`.
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