Rust Lesson 8 – Tuples and Arrays | Dataplexa

Tuples and Arrays in Rust

In this lesson, you will learn about tuples and arrays in Rust. Both are compound data types used to group multiple values together.

Although tuples and arrays may look similar at first, they serve different purposes and follow different rules.


What Are Tuples?

A tuple is a collection of values grouped together into a single variable.

Unlike arrays, tuples can store values of different data types. Once created, the size of a tuple cannot change.


Creating a Tuple

Tuples are created by placing values inside parentheses and separating them with commas.

fn main() {
    let person = ("Alice", 30, true);
    println!("{}", person.0);
    println!("{}", person.1);
}

Each value in the tuple is accessed using a dot followed by its index.


Destructuring Tuples

Rust allows you to unpack tuple values into separate variables. This process is called destructuring.

fn main() {
    let data = ("Rust", 2025, true);
    let (name, year, active) = data;

    println!("{}", name);
    println!("{}", year);
    println!("{}", active);
}

Destructuring improves readability when working with complex tuples.


Single-Value Tuples

A tuple with a single value must include a trailing comma.

fn main() {
    let single = (5,);
    println!("{}", single.0);
}

Without the comma, Rust treats the value as a normal integer, not a tuple.


What Are Arrays?

An array is a collection of values of the same data type.

Arrays in Rust have a fixed size that must be known at compile time.


Creating an Array

Arrays are created using square brackets.

fn main() {
    let numbers = [10, 20, 30, 40, 50];
    println!("{}", numbers[0]);
}

Array indexing starts at 0.


Array Type Annotation

You can explicitly define the type and size of an array.

fn main() {
    let values: [i32; 3] = [1, 2, 3];
    println!("{}", values[1]);
}

Here, [i32; 3] means an array of three integers.


Initializing Arrays with Default Values

Rust provides a shorthand to initialize an array with the same value.

fn main() {
    let zeros = [0; 5];
    println!("{}", zeros[2]);
}

This creates an array of five elements, all initialized to zero.


Mutability in Arrays

Arrays can be mutable if declared using the mut keyword.

fn main() {
    let mut numbers = [1, 2, 3];
    numbers[1] = 10;
    println!("{}", numbers[1]);
}

Only mutable arrays allow updating values.


Tuples vs Arrays

Key differences between tuples and arrays:

  • Tuples can store different data types
  • Arrays store values of the same type
  • Tuples are accessed using dot notation
  • Arrays are accessed using index notation

📝 Practice Exercises


Exercise 1

Create a tuple with three values of different types.

Exercise 2

Destructure a tuple into separate variables.

Exercise 3

Create an array of five numbers and access one element.

Exercise 4

Create a mutable array and update one value.


✅ Practice Answers


Answer 1

fn main() {
    let info = ("Rust", 1.0, true);
    println!("{}", info.0);
}

Answer 2

fn main() {
    let data = ("Data", 100);
    let (name, value) = data;
    println!("{}", name);
}

Answer 3

fn main() {
    let nums = [5, 10, 15, 20, 25];
    println!("{}", nums[3]);
}

Answer 4

fn main() {
    let mut values = [1, 2, 3];
    values[0] = 9;
    println!("{}", values[0]);
}

What’s Next?

Now that you understand tuples and arrays, you are ready to learn about operators in Rust.

In the next lesson, you will explore arithmetic, comparison, and logical operators.