Compound types
Compound types group values: tuples, arrays, slices, structs, and enums. They have a known layout at compile time and are the way Rust builds larger values out of scalars.
Detailed Explanation
A scalar holds one number, one boolean, or one character. Almost every program needs more than that: a coordinate, a buffer of bytes, a record with a name and an id. Rust calls those compound types.
The two built-in compounds you meet first are the tuple and the array. A tuple groups values of possibly different types: (u16, String) is a port and a host. You index a tuple with a dot and a literal, pair.0, because the index is part of the type. A tuple's length is fixed and each position has its own type, so (u16, String) and (String, u16) are different types. The unit type () is the empty tuple, and it is what a function returns when it returns nothing of interest.
An array groups values of the same type and a length that is part of the type: [u8; 4] is four bytes. The length is a compile-time constant, so [u8; 4] and [u8; 5] cannot be used interchangeably. Arrays live on the stack. When you need a length that changes, you use Vec<T> or a slice &[T]. A slice is a fat pointer — address plus length — into some contiguous storage you do not own, and it is the type you want for "a run of elements I will only read".
Structs and enums are the user-defined compounds. A struct names its fields. An enum names a closed set of variants, each of which may carry data. Those two are how you model domain data. Tuples and arrays are how you group things locally when a named type would be ceremony.
Why the distinction matters: Rust has no implicit conversion between compounds that "look similar". A 2-tuple is not an array of two elements. A struct with two u32 fields is not a (u32, u32) unless you write the conversion. That rigidity is what lets the compiler lay values out and move them without guessing. The cost is a few more named types and a few more .into() calls than a dynamically typed language would ask for.
When you reach for a tuple: a function that returns two values you will unpack immediately. When you reach for an array: a fixed-size buffer, a lookup table, a colour as [u8; 3]. When the grouping has meaning that will travel across functions, promote it to a struct. When the grouping is "one of several shapes", use an enum.
Code Examples
fn main() {
let endpoint: (u16, &str) = (8080, "localhost");
println!("{}:{}", endpoint.1, endpoint.0); // prints: localhost:8080
let rgb: [u8; 3] = [12, 80, 210];
let first = rgb[0];
println!("{first}"); // prints: 12
// A slice borrows the array. The length is stored in the reference, not the type.
let channels: &[u8] = &rgb[1..];
println!("{}", channels.len()); // prints: 2
}Common Gotchas
Tuple indexes must be literals
`pair.i` where `i` is a variable does not compile. Destructure, or use an array if you need a runtime index.
Array length is part of the type
A function that takes `[u8; 4]` will not accept `[u8; 16]`. Take a slice `&[u8]` when the length varies.
Out-of-bounds array index panics
`rgb[3]` on a `[u8; 3]` is a runtime panic, not a compile error, when the index is not a constant the compiler can see.
Prefer a struct once a tuple grows
A 4-tuple of mixed types is unreadable at the call site. Name the fields.