Understanding Compound Types in Rust

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.

What are Compound Types?

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.

Tuples

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.

Characteristics of Tuples:

  • Fixed Length: Once a tuple is declared, its length is fixed. You cannot add or remove elements from a tuple after it is created.
  • Heterogeneous Types: A tuple can contain values of different types. For example, you can have a tuple that holds an integer, a floating-point number, and a string.
  • Arity: The number of elements in a tuple is known as its arity. For instance, a tuple with three elements has an arity of three.

Declaring and Accessing Tuples:

Here’s how you can declare and access tuples in Rust:

let tup: (i32, f64, u8) = (500, 6.4, 1);
Rust

In 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;
Rust

You can also use pattern matching to de-structure the tuple into individual variables:

let (first_element, second_element, third_element) = tup;
Rust

Limitations:

Rust 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

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.

Characteristics of Arrays:

  • Fixed Length: Like tuples, arrays have a fixed length. Once declared, the size of an array cannot be changed.
  • Homogeneous Elements: All elements in an array must be of the same type.
  • Inline Storage: An array stores its elements contiguously within the array value. Its location depends on where that value is stored; an array can also be placed on the heap, for example inside a Box.

Declaring and Accessing Arrays:

Here’s how you can declare and access arrays in Rust:

let arr = [1, 2, 3, 4, 5];
Rust

In 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];
Rust

To access elements in an array, you use index notation, where indexing starts at 0:

let first_element = arr[0]; // This will give you 1
Rust

Arrays are useful for scenarios where you need a collection of elements that are tightly packed in memory, as they are stored contiguously.

Conclusion

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.

Share
Prabhat Kashyap

Prabhat Kashyap

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.

Newsletter

Get new engineering deep dives

Scala, JVM, AI, and backend systems. I send practical articles when there is something worth reading.

Join the newsletter