File tree 1 file changed +8
-3
lines changed
1 file changed +8
-3
lines changed Original file line number Diff line number Diff line change @@ -162,13 +162,18 @@ A ‘slice’ is a reference to (or “view” into) another data structure. The
162
162
useful for allowing safe, efficient access to a portion of an array without
163
163
copying. For example, you might want to reference just one line of a file read
164
164
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:
167
172
168
173
``` rust
169
174
let a = [0 , 1 , 2 , 3 , 4 ];
170
- let middle = & a [1 .. 4 ]; // A slice of a: just the elements 1, 2, and 3
171
175
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
172
177
```
173
178
174
179
Slices have type ` &[T] ` . We’ll talk about that ` T ` when we cover
You can’t perform that action at this time.
0 commit comments