1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-06-26 18:51:00 +02:00

Merge pull request #163 from rastringer/patch-1

Speaker notes to 6.4 Slices
This commit is contained in:
Martin Geisler
2023-01-12 17:16:57 +01:00
committed by GitHub

View File

@ -14,3 +14,17 @@ fn main() {
* Slices borrow data from the sliced type. * Slices borrow data from the sliced type.
* Question: What happens if you modify `a[3]`? * Question: What happens if you modify `a[3]`?
<details>
* We create a slice by borrowing `a` and specifying the starting and ending indexes in brackets.
* If the slice starts at index 0, Rust’s range syntax means we can drop the starting index.
* The same is true for the last index, so `&a[2..a.len()]` and `&a[2..]` are equal.
* `s` is a reference to a slice of `i32`s. Notice that the type of `s` no longer mentions the array length. This allows us to performing computations on slices of different sizes.
* Slices always borrow from another object. In this example, `a` has to remain 'alive' so we can take a slice from it.
</details>