Group values with tuples or arrays, access their elements, and destructure tuples. Learn the difference between a tuple’s length and standard-library trait support.
Group values with tuples or arrays, access their elements, and destructure tuples. Learn the difference between a tuple’s length and standard-library trait support.
Rust’s compound types group values together. A tuple can contain different types, while an array contains a fixed number of elements of one type. After scalar types, these are the next building blocks for representing data.
Compound types in Rust are types that can hold multiple values. These values can be of different types (in the case of tuples) or the same type (in the case of arrays). Compound types are useful when you need to group related data together in a structured way.
A tuple groups multiple values into one compound value. Its elements can have different types, unlike the arrays covered in the next section. Tuples are useful for returning several values from a function or keeping a small group of related values together.
Here’s how you can declare and access tuples in Rust:
let tup: (i32, f64, u8) = (500, 6.4, 1);RustIn this example, tup is a tuple that holds an i32, an f64, and a u8 value. You can access the elements of the tuple using dot notation, with the index of the element you want:
let first_element = tup.0;
let second_element = tup.1;
let third_element = tup.2;RustYou can also use pattern matching to de-structure the tuple into individual variables:
let (first_element, second_element, third_element) = tup;RustRust tuples can have more than 12 elements. The restriction applies to some standard-library trait implementations, including Debug and PartialEq, rather than to tuple syntax itself. Check the tuple trait documentation when using a large tuple. For a large group of named fields, a struct is usually easier to understand.
Arrays in Rust are collections of elements of the same type. Unlike tuples, arrays are homogeneous, meaning all elements must be of the same type. Arrays are particularly useful when you need to store a fixed-size list of elements, such as the days of the week, or a series of numbers.
Box.Here’s how you can declare and access arrays in Rust:
let arr = [1, 2, 3, 4, 5];RustIn this example, arr is an array of five i32 elements. You can also explicitly specify the type and length of the array:
let arr: [i32; 5] = [1, 2, 3, 4, 5];RustTo access elements in an array, you use index notation, where indexing starts at 0:
let first_element = arr[0]; // This will give you 1RustArrays are useful for scenarios where you need a collection of elements that are tightly packed in memory, as they are stored contiguously.
Use a tuple for a small, fixed group of values that may have different types. Use an array for a fixed-length sequence of one type. Continue with Rust functions to pass these values into functions, or control flow and loops to iterate over an array.

Senior Technical Architect at HCLTech · working with Leonteq Security AG
I have 10+ years of experience building distributed systems and fintech platforms. I write about practical, non-obvious engineering details that official documentation often skips.
Scala, JVM, AI, and backend systems. I send practical articles when there is something worth reading.
Join the newsletter