Rust
December 2, 2026 4 min read

Array and tuple in rust

HS
HEMANT SINGH
Technical Writer & Educator

Lesson 7: Arrays and Tuples in Rust

1. What is an Array?

An array is a collection of multiple values stored in a single variable.

In Rust, an array has two important characteristics:

  1. All elements must have the same data type.
  2. The size of an array is fixed.

Example

fn main() {
let numbers = [10, 20, 30, 40, 50];

println!("{:?}", numbers);
}

Output:

[10, 20, 30, 40, 50]

Here, numbers is an array containing five integer values.

The {:?} format specifier is used to print the array in debug format.

2. Array Syntax

let array_name = [value1, value2, value3];

Example:

let fruits = ["Apple", "Mango", "Banana"];

This array contains three string slices.

3. Accessing Array Elements

We can access individual elements using their index.

Important: Array indexing starts from 0, not 1.

Example:

fn main() {
let numbers = [10, 20, 30, 40, 50];

println!("First: {}", numbers[0]);
println!("Second: {}", numbers[1]);
println!("Third: {}", numbers[2]);
}

Output:

First: 10
Second: 20
Third: 30

Array Index Positions


Index

Value

0

10

1

20

2

30

3

40

4

50

4. Modifying Array Elements

By default, variables in Rust are immutable.

To modify array elements, declare the array using mut.

Example:

fn main() {
let mut numbers = [10, 20, 30];

numbers[0] = 100;

println!("{:?}", numbers);
}

Output:

[100, 20, 30]

Remember: You can change existing elements, but you cannot increase or decrease the array's length.

5. Array with Explicit Data Type

You can specify the data type of an array.

Example:

fn main() {
let numbers: [i32; 4] = [10, 20, 30, 40];

println!("{:?}", numbers);
}

Here:

  1. i32 is the data type of each element.
  2. 4 is the number of elements.

The syntax is:

let array_name: [data_type; length] = [values];

6. Creating an Array with Repeated Values

Rust allows you to create an array with the same value repeated multiple times.

Syntax:

let array_name = [value; length];

Example:

fn main() {
let numbers = [0; 5];

println!("{:?}", numbers);
}

Output:

[0, 0, 0, 0, 0]

Here, 0 is repeated five times.

Another example:

let marks = [100; 3];

Output:

[100, 100, 100]

7. Finding the Length of an Array

Use the .len() method to find the number of elements in an array.

Example:

fn main() {
let numbers = [10, 20, 30, 40, 50];

println!("Length: {}", numbers.len());
}

Output:

Length: 5

8. Looping Through an Array

We can use a for loop to access every element of an array.

Example:

fn main() {
let numbers = [10, 20, 30, 40, 50];

for number in numbers {
println!("{}", number);
}
}

Output:

10
20
30
40
50

Looping Through Array Indexes

Use .iter().enumerate() when you need both the index and the value.

Example:

fn main() {
let fruits = ["Apple", "Mango", "Banana"];

for (index, fruit) in fruits.iter().enumerate() {
println!("{}: {}", index, fruit);
}
}

Output:

0: Apple
1: Mango
2: Banana

9. What is a Tuple?

A tuple is a collection of multiple values stored together.

Unlike arrays, a tuple can contain values of different data types.

Example:

fn main() {
let student = ("Hemant", 32, 85.5, true);

println!("{:?}", student);
}

Output:

("Hemant", 32, 85.5, true)

Here, the tuple contains:

  1. A string slice
  2. An integer
  3. A floating-point number
  4. A Boolean value

10. Tuple Syntax

let tuple_name = (value1, value2, value3);

Example:

let person = ("Rahul", 25, true);

A tuple can contain different data types.

11. Accessing Tuple Elements

We can access tuple elements using their position and dot notation.

Example:

fn main() {
let student = ("Hemant", 32, 85.5);

println!("Name: {}", student.0);
println!("Age: {}", student.1);
println!("Marks: {}", student.2);
}

Output:

Name: Hemant
Age: 32
Marks: 85.5

Remember:

  1. student.0 accesses the first element.
  2. student.1 accesses the second element.
  3. student.2 accesses the third element.

12. Tuple Destructuring

We can extract tuple values into separate variables using destructuring.

Example:

fn main() {
let student = ("Hemant", 32, 85.5);

let (name, age, marks) = student;

println!("Name: {}", name);
println!("Age: {}", age);
println!("Marks: {}", marks);
}

Output:

Name: Hemant
Age: 32
Marks: 85.5

This makes tuple values easier to use.

13. Mutable Tuple

We can modify tuple elements when the tuple variable is declared with mut.

Example:

fn main() {
let mut student = ("Hemant", 32);

student.1 = 33;

println!("{:?}", student);
}

Output:

("Hemant", 33)

The tuple's element types and structure cannot be changed after declaration.

14. Unit Tuple

An empty tuple () is called the unit value.

It represents the absence of a meaningful value.

Example:

fn main() {
let result = ();

println!("{:?}", result);
}

Output:

()

Functions that do not explicitly return another value return the unit type ().

Example:

fn greet() {
println!("Hello!");
}

The return type of greet() is ().

15. Difference Between Array and Tuple


Feature

Array

Tuple

Data types

Same type

Different types allowed

Size

Fixed

Fixed

Access

Index [0]

Position .0

Example

[10, 20, 30]

("Hemant", 32, true)

Use

Collection of similar values

Group of related, different values

16. Arrays and Tuples with Functions

Arrays and tuples can also be passed to functions.

Passing an Array

fn display(numbers: [i32; 3]) {
println!("{:?}", numbers);
}

fn main() {
let values = [10, 20, 30];

display(values);
}

Output:

[10, 20, 30]

Returning a Tuple

fn get_student() -> (&'static str, i32) {
("Hemant", 32)
}

fn main() {
let student = get_student();

println!("Name: {}", student.0);
println!("Age: {}", student.1);
}

Output:

Name: Hemant
Age: 32

17. Important Notes

  1. Array indexing starts from zero.
  2. Arrays have a fixed length.
  3. Every array element must have the same type.
  4. Tuples can contain different data types.
  5. Use .len() to find the length of an array.
  6. Use {:?} to print arrays and tuples in debug format.
  7. Use mut when you need to modify array or tuple elements.
  8. Accessing an array using an invalid index causes a runtime panic.

18. Quick Reference


Operation

Array

Tuple

Create

let a = [10, 20, 30];

let t = (10, "Rust", true);

First element

a[0]

t.0

Second element

a[1]

t.1

Length

a.len()

No direct .len() method

Modify

a[0] = 100;

t.0 = 100;