Expressions & Flow · 11 min · free lesson

Compound Types

Rust checks scalar and compound types before the program runs.

Core idea

A tuple mixes types by position; an array holds one type with a fixed length.

Start with the idea

Tuples group a fixed number of possibly different types and are read by position. Arrays hold one type with a length known at compile time, which is exactly what lets the compiler bounds-check them and reject an out-of-range index it can see.

Read the code

let point: (i32, f64) = (3, 1.5);
let scores: [u8; 4] = [90, 75, 60, 88];
println!("{} {}", point.0, scores[2]);

Position for a tuple, index for an array.

Practice starter

let items: [i32; 3] = [1, 2, 3];
let value = items[5];

Practise this code in Playground ↗

Diagram for Compound Types: a Rust code example flows to the lesson rule and the key observation.
Position for a tuple, index for an array.

Work through it

The point in the example is a tuple: its first field is an i32 and its second is an f64. The fields are read as point.0 and point.1 because their positions, not names, identify them. The scores array is different: all four elements are u8, and the length 4 belongs to the type [u8; 4]. That makes scores[2] a predictable access to the third score. Before using an index, ask whether the index is a constant the compiler can reject or a runtime value that needs a check.

Tuples help when a few values belong together for a short journey through code. Arrays help when you have a fixed-size sequence of one kind of item. Neither is a general substitute for a struct with meaningful field names or a Vec that must grow. Choosing the shape is part of explaining the data, not merely satisfying syntax. In a function signature, (i32, f64) tells callers exactly what comes back, while [u8; 4] promises exactly four scores.

Try changing the tuple field order and observe which accesses now need a different type. Then change the array length annotation without changing its elements. Compare the two diagnostics: one concerns the type at a position, the other concerns the number of elements. Predict whether an out-of-range constant index will be caught before execution, and distinguish that case from an index read from user input.

A type annotation can document intent at a public boundary or resolve an otherwise ambiguous parse. Inside a small expression, inference usually keeps the code lighter. Add annotations where they help the reader or compiler choose, not mechanically to every binding.

A type tells the compiler which operations a value supports and how it is represented. i32 is a signed 32-bit integer, bool is true or false, and &str is borrowed UTF-8 text. A tuple groups values by position; an array stores a fixed number of values of one element type. Read a type annotation from the inside out instead of trying to memorize every symbol at once. For [i32; 3], the element type is i32 and the length is three.

Inference works when the surrounding code gives enough information, but an explicit annotation can document intent or settle ambiguity. Try changing a tuple element from a number to text and see which later operations stop compiling. Then compare an array with a Vec: an array's length is part of its type, while a vector can grow. When a type error appears, find the expression that produces the value and the place that expects it. The difference between those two is the decision you need to make.

Try it

Build one tuple and one array, then read a value from each.

What success looks like: The output shows both values with no type mismatch.

Check your understanding

What happens with that index?

Reveal the answer
  1. It compiles and panics at runtime with an index error
  2. The compiler rejects an index it can prove is out of range
  3. It wraps around to the first element like a ring buffer

A constant index past a known length is a compile error, not a runtime surprise.

One more experiment

Change scores[2] to scores[4] and predict rustc's response. Then change point.0 to point.2 and compare the two diagnostics: one concerns the array length, the other the tuple's fields.

Learn this lesson with AI

Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.

I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools.
Lesson: Compound Types (courseId "basics", lessonId "types")
Link: https://crabrix.com/learn/basics/types/
Goal: A tuple mixes types by position; an array holds one type with a fixed length.
Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=basics/types) and I will write the code there.
Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.

In Claude Code, Codex or Cursor, paste it into a chat with Crabrix Academy connected. On a phone, the Claude and ChatGPT apps open the same links.