1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2024-12-12 05:24:12 +02:00

Speaker notes to 6.4 Slices

Brief digest of the slice type, including mentions of borrowing, range syntax and lifetimes.
This commit is contained in:
Robin Stringer 2023-01-12 15:53:16 +00:00 committed by GitHub
parent 39693b929b
commit f4b651456c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,3 +14,17 @@ fn main() {
* Slices borrow data from the sliced type.
* 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 `let s: &[i32] = &a[2..len];` or `let s: &[i32] = &a[2..];` are equal.
* We set `s` as a reference of `i32`s. Notice that the type of `s` no longer has an array length. This avoids type errors when 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>