Topic
Tuples
A tuple groups values of different types together in a single variable. Unlike arrays, each element can be a different type. You access elements by position using dot notation, or unpack everything at once with destructuring. Tuples are useful any time you need to pass or return a few related values without defining a full struct.
Quick Reference
| Concept | What it means |
|---|---|
| Tuple | A single variable that holds multiple values of different types grouped together. |
| (i32, &str) | Tuple type signature: parentheses with the types inside, separated by commas. |
| t.0 | Tuple indexing: the first element is at index 0, the second at 1, and so on. |
| Destructuring | Pulling a tuple apart into separate variables with let (a, b) = t; |
| Nested tuple | A tuple inside another tuple, like (u8, (i32, i32)). |
| Term | What it means | When you use it |
|---|---|---|
| Tuple | A single variable holding multiple values of different types | When grouping a few related values without defining a struct |
| (i32, &str) | Tuple type: parentheses with types inside, separated by commas | When declaring or annotating a tuple variable |
| t.0 | Tuple indexing: first element is at 0, second at 1, and so on | When extracting one specific element by position |
| Destructuring | Pulling a tuple apart into separate variables with let (a, b) = t; | When you need all elements as separate named variables |
| Nested tuple | A tuple inside another tuple, like (u8, (i32, i32)) | When grouping within a group, like (address, (balance, nonce)) |