Skip to content

Commit bc0440a

Browse files
committed
Call out slicing syntax more explicitly
Fixes rust-lang#28359
1 parent 3e6d724 commit bc0440a

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/doc/trpl/primitive-types.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,18 @@ A ‘slice’ is a reference to (or “view” into) another data structure. The
162162
useful for allowing safe, efficient access to a portion of an array without
163163
copying. For example, you might want to reference just one line of a file read
164164
into memory. By nature, a slice is not created directly, but from an existing
165-
variable. Slices have a length, can be mutable or not, and in many ways behave
166-
like arrays:
165+
variable binding. Slices have a defined length, can be mutable or immutable.
166+
167+
## Slicing syntax
168+
169+
You can use a combo of `&` and `[]` to create a slice from various things. The
170+
`&` indicates that slices are similar to references, and the `[]`s, with a
171+
range, let you define the length of the slice:
167172

168173
```rust
169174
let a = [0, 1, 2, 3, 4];
170-
let middle = &a[1..4]; // A slice of a: just the elements 1, 2, and 3
171175
let complete = &a[..]; // A slice containing all of the elements in a
176+
let middle = &a[1..4]; // A slice of a: just the elements 1, 2, and 3
172177
```
173178

174179
Slices have type `&[T]`. We’ll talk about that `T` when we cover

0 commit comments

Comments
 (0)