Tuples and fixed-size arrays
Tuples and fixed-size arrays
A student record might pair a name with a mark, while three marks form a fixed-size collection. A tuple can contain different types. An array contains values of one type and has a length that forms part of its type. These are different ways to organize a small amount of data.
What you’ll learn
- Organize small records and fixed-size collections.
- Use zero-based positions and handle a missing element.
Tuples and fixed-size arrays in action
Tuple: name plus mark
The tuple has a text field and an integer field.
Tuple: name plus markArray indexes: 0, 1, 2
Destructuring reads them into name and score.
Array indexes: 0, 1, 2get(3): None
The three-element array has indexes 0, 1 and 2; get(3) reports absence rather than returning a fourth mark.
get(3): None
Tuples and fixed-size arrays explained
Destructuring assigns tuple fields to names. Array indexing starts at zero, so index 0 refers to the first element. Direct indexing can panic when the index is out of bounds; get returns an Option you can handle. A growable collection will use Vec in a later lesson.
A collection length uses usize, an unsigned integer type appropriate for sizes and indexes on the target platform. Do not assume it has the same width on every platform.
Work through the Rust program
Save the complete source as main.rs in the Rust environment. No standard input is needed.
Rust 2024 · complete program
fn main() {
let student = ("Diya", 80);
let (name, score) = student;
let marks = [70, 80, 90];
println!("{name}: {score}");
println!("First: {}", marks[0]);
println!("{:?}", marks.get(3));
}Expected output
Diya: 80
First: 70
NoneFollow the execution
The tuple has a text field and an integer field. Destructuring reads them into name and score. The three-element array has indexes 0, 1 and 2; get(3) reports absence rather than returning a fourth mark.
Try the complete program in the Rust compiler. For tests, Cargo commands and local files, use the specified local environment.
Try a small variation
Your task: Which indexes are valid for this three-element array?
Rust 2024 · complete program
fn main() {
let student = ("Diya", 80);
let (name, score) = student;
let marks = [70, 80, 90];
println!("{name}: {score}");
println!("First: {}", marks[0]);
println!("{:?}", marks.get(3));
}Worked answer
Indexes zero, one and two are valid. The length is three, but index three is immediately beyond the last element.
Mistakes and limits to recognize
Arrays have fixed lengths; they do not grow when indexed. Use descriptive structs when tuple positions become hard to remember. A tuple type can be Copy only when each field supports Copy.
Check your understanding
Take the idea into your next program
Indexes zero, one and two are valid. The length is three, but index three is immediately beyond the last element. Arrays have fixed lengths; they do not grow when indexed.
Further reading: the official Rust Book on tuples and fixed-size arrays.
Reaching the recap and completing the quizzes are separate milestones.