Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Divided Array and tuples #1859

Merged
merged 2 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ use-boolean-and = true
#
# Please keep the table sorted and avoid multi-step redirects.
[output.html.redirect]
"tuples-and-arrays/tuples-and-arrays.html" = "tuples.html"
"async/concurrency/channels.html" = "../channels.html"
"async/pitfall/async-traits.html" = "../pitfalls/async-traits.html"
"basic-syntax.html" = "control-flow-basics.html"
Expand Down
3 changes: 2 additions & 1 deletion src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@

- [Welcome](welcome-day-1-afternoon.md)
- [Tuples and Arrays](tuples-and-arrays.md)
- [Tuples and Arrays](tuples-and-arrays/tuples-and-arrays.md)
- [Arrays](tuples-and-arrays/arrays.md)
- [Tuples](tuples-and-arrays/tuples.md)
- [Array Iteration](tuples-and-arrays/iteration.md)
- [Patterns and Destructuring](tuples-and-arrays/destructuring.md)
- [Exercise: Nested Arrays](tuples-and-arrays/exercise.md)
Expand Down
39 changes: 39 additions & 0 deletions src/tuples-and-arrays/arrays.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
minutes: 5
---

# Arrays

<!-- mdbook-xgettext: skip -->

```rust,editable
fn main() {
let mut a: [i8; 10] = [42; 10];
a[5] = 0;
println!("a: {a:?}");
}
```

<details>

- 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.

- Try accessing an out-of-bounds array element. Array accesses are checked at
runtime. Rust can usually optimize these checks away, and they can be avoided
using unsafe Rust.

- We can use literals to assign values to arrays.

- 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.

- Adding `#`, eg `{a:#?}`, invokes a "pretty printing" format, which can be
easier to read.

</details>
82 changes: 0 additions & 82 deletions src/tuples-and-arrays/tuples-and-arrays.md

This file was deleted.

29 changes: 29 additions & 0 deletions src/tuples-and-arrays/tuples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
minutes: 5
---

# Tuples

<!-- mdbook-xgettext: skip -->

```rust,editable
fn main() {
let t: (i8, bool) = (7, true);
println!("t.0: {}", t.0);
println!("t.1: {}", t.1);
}
```

<details>

- Like arrays, tuples have a fixed length.

- Tuples group together values of different types into a compound type.

- Fields of a tuple can be accessed by the period and the index of the value,
e.g. `t.0`, `t.1`.

- The empty tuple `()` is referred to as the "unit type" and signifies absence
of a return value, akin to `void` in other languages.

</details>