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:
- All elements must have the same data type.
- The size of an array is fixed.
Example
Output:
Here, numbers is an array containing five integer values.
The {:?} format specifier is used to print the array in debug format.
2. Array Syntax
Example:
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:
Output:
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:
Output:
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:
Here:
i32is the data type of each element.4is the number of elements.
The syntax is:
6. Creating an Array with Repeated Values
Rust allows you to create an array with the same value repeated multiple times.
Syntax:
Example:
Output:
Here, 0 is repeated five times.
Another example:
Output:
7. Finding the Length of an Array
Use the .len() method to find the number of elements in an array.
Example:
Output:
8. Looping Through an Array
We can use a for loop to access every element of an array.
Example:
Output:
Looping Through Array Indexes
Use .iter().enumerate() when you need both the index and the value.
Example:
Output:
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:
Output:
Here, the tuple contains:
- A string slice
- An integer
- A floating-point number
- A Boolean value
10. Tuple Syntax
Example:
A tuple can contain different data types.
11. Accessing Tuple Elements
We can access tuple elements using their position and dot notation.
Example:
Output:
Remember:
student.0accesses the first element.student.1accesses the second element.student.2accesses the third element.
12. Tuple Destructuring
We can extract tuple values into separate variables using destructuring.
Example:
Output:
This makes tuple values easier to use.
13. Mutable Tuple
We can modify tuple elements when the tuple variable is declared with mut.
Example:
Output:
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:
Output:
Functions that do not explicitly return another value return the unit type ().
Example:
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 | Position |
Example |
|
|
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
Output:
Returning a Tuple
Output:
17. Important Notes
- Array indexing starts from zero.
- Arrays have a fixed length.
- Every array element must have the same type.
- Tuples can contain different data types.
- Use
.len()to find the length of an array. - Use
{:?}to print arrays and tuples in debug format. - Use
mutwhen you need to modify array or tuple elements. - Accessing an array using an invalid index causes a runtime panic.
18. Quick Reference
Operation | Array | Tuple |
|---|---|---|
Create |
|
|
First element |
|
|
Second element |
|
|
Length |
| No direct |
Modify |
|
|