2023-11-29 10:39:24 -05:00
|
|
|
---
|
|
|
|
minutes: 10
|
|
|
|
---
|
|
|
|
|
|
|
|
# Tuples and Arrays
|
|
|
|
|
|
|
|
Tuples and arrays are the first "compound" types we have seen. All elements of
|
2023-12-31 00:15:07 +01:00
|
|
|
an array have the same type, while tuples can accommodate different types. Both
|
|
|
|
types have a size fixed at compile time.
|
2022-12-21 16:36:30 +01:00
|
|
|
|
2022-12-28 15:41:08 +01:00
|
|
|
| | Types | Literals |
|
2023-12-31 00:15:07 +01:00
|
|
|
| ------ | ----------------------------- | --------------------------------- |
|
2022-12-28 15:41:08 +01:00
|
|
|
| Arrays | `[T; N]` | `[20, 30, 40]`, `[0; 3]` |
|
|
|
|
| Tuples | `()`, `(T,)`, `(T1, T2)`, ... | `()`, `('x',)`, `('x', 1.2)`, ... |
|
2022-12-21 16:36:30 +01:00
|
|
|
|
|
|
|
Array assignment and access:
|
|
|
|
|
2023-09-26 17:04:46 +02:00
|
|
|
<!-- mdbook-xgettext: skip -->
|
2023-12-31 00:15:07 +01:00
|
|
|
|
2022-12-21 16:36:30 +01:00
|
|
|
```rust,editable
|
|
|
|
fn main() {
|
|
|
|
let mut a: [i8; 10] = [42; 10];
|
|
|
|
a[5] = 0;
|
2023-10-24 11:32:34 +02:00
|
|
|
println!("a: {a:?}");
|
2022-12-21 16:36:30 +01:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Tuple assignment and access:
|
|
|
|
|
2023-09-26 17:04:46 +02:00
|
|
|
<!-- mdbook-xgettext: skip -->
|
2023-12-31 00:15:07 +01:00
|
|
|
|
2022-12-21 16:36:30 +01:00
|
|
|
```rust,editable
|
|
|
|
fn main() {
|
|
|
|
let t: (i8, bool) = (7, true);
|
2023-09-26 17:04:46 +02:00
|
|
|
println!("t.0: {}", t.0);
|
|
|
|
println!("t.1: {}", t.1);
|
2022-12-21 16:36:30 +01:00
|
|
|
}
|
|
|
|
```
|
2023-01-09 14:18:37 +00:00
|
|
|
|
|
|
|
<details>
|
2023-01-12 18:51:37 +01:00
|
|
|
|
2023-01-09 14:18:37 +00:00
|
|
|
Key points:
|
2023-01-12 18:51:37 +01:00
|
|
|
|
2023-01-09 14:18:37 +00:00
|
|
|
Arrays:
|
2023-01-12 18:51:37 +01:00
|
|
|
|
2023-12-31 00:15:07 +01:00
|
|
|
- A value of the array type `[T; N]` holds `N` (a compile-time constant)
|
|
|
|
elements of the same type `T`. Note that the length of the array is _part of
|
|
|
|
its type_, which means that `[u8; 3]` and `[u8; 4]` are considered two
|
|
|
|
different types. Slices, which have a size determined at runtime, are covered
|
|
|
|
later.
|
2023-11-29 10:39:24 -05:00
|
|
|
|
2023-12-31 00:15:07 +01:00
|
|
|
- Try accessing an out-of-bounds array element. Array accesses are checked at
|
2023-11-29 10:39:24 -05:00
|
|
|
runtime. Rust can usually optimize these checks away, and they can be avoided
|
|
|
|
using unsafe Rust.
|
2023-01-09 14:18:37 +00:00
|
|
|
|
2023-12-31 00:15:07 +01:00
|
|
|
- We can use literals to assign values to arrays.
|
2023-01-09 14:18:37 +00:00
|
|
|
|
2023-12-31 00:15:07 +01:00
|
|
|
- The `println!` macro asks for the debug implementation with the `?` format
|
|
|
|
parameter: `{}` gives the default output, `{:?}` gives the debug output. Types
|
|
|
|
such as integers and strings implement the default output, but arrays only
|
|
|
|
implement the debug output. This means that we must use debug output here.
|
2023-01-09 14:18:37 +00:00
|
|
|
|
2023-12-31 00:15:07 +01:00
|
|
|
- Adding `#`, eg `{a:#?}`, invokes a "pretty printing" format, which can be
|
|
|
|
easier to read.
|
2023-01-09 14:18:37 +00:00
|
|
|
|
|
|
|
Tuples:
|
|
|
|
|
2023-12-31 00:15:07 +01:00
|
|
|
- Like arrays, tuples have a fixed length.
|
2023-01-09 14:18:37 +00:00
|
|
|
|
2023-12-31 00:15:07 +01:00
|
|
|
- Tuples group together values of different types into a compound type.
|
2023-01-09 14:18:37 +00:00
|
|
|
|
2023-12-31 00:15:07 +01:00
|
|
|
- Fields of a tuple can be accessed by the period and the index of the value,
|
|
|
|
e.g. `t.0`, `t.1`.
|
2023-01-12 18:51:37 +01:00
|
|
|
|
2023-12-31 00:15:07 +01:00
|
|
|
- The empty tuple `()` is also known as the "unit type". It is both a type, and
|
|
|
|
the only valid value of that type --- that is to say both the type and its
|
|
|
|
value are expressed as `()`. It is used to indicate, for example, that a
|
|
|
|
function or expression has no return value, as we'll see in a future slide.
|
|
|
|
- You can think of it as `void` that can be familiar to you from other
|
|
|
|
programming languages.
|
2023-01-09 14:18:37 +00:00
|
|
|
|
|
|
|
</details>
|