File tree Expand file tree Collapse file tree 1 file changed +16
-6
lines changed Expand file tree Collapse file tree 1 file changed +16
-6
lines changed Original file line number Diff line number Diff line change @@ -229,28 +229,38 @@ assert(*p == 2);
229
229
230
230
$(P See $(DDSUBLINK spec/expression, pointer_arithmetic, *AddExpression*) for details.)
231
231
232
+
232
233
$(H2 $(LNAME2 slicing, Slicing))
233
234
234
235
$(P $(I Slicing) an array means to specify a subarray of it.
236
+ This is done by supplying two index expressions.
237
+ The elements from the start index up until the end index are selected.
238
+ Any item at the end index is not included.
239
+ )
240
+ $(P
235
241
An array slice does not copy the data, it is only another
236
242
reference to it. Slicing produces a dynamic array.
237
- For example:
238
243
)
239
244
240
245
$(SPEC_RUNNABLE_EXAMPLE_RUN
241
246
---------
242
- int[10 ] a; // declare array of 10 ints
247
+ int[3 ] a = [4, 5, 6]; // static array of 3 ints
243
248
int[] b;
244
249
245
- b = a[1..3]; // a[1..3] is a 2 element array consisting of
250
+ b = a[1..3]; // a[1..3] is a 2 element dynamic array consisting of
246
251
// a[1] and a[2]
247
- assert(b[1] == 0);
252
+ assert(b == [5, 6]);
253
+ assert(b.ptr == a.ptr + 1);
254
+
248
255
a[2] = 3;
249
- assert(b[1] == 3);
256
+ assert(b == [5, 3]);
257
+
258
+ b = b[1..2];
259
+ assert(b == [3]);
250
260
---------
251
261
)
252
262
253
- $(P $(I Identifier)[] is shorthand for a slice of the entire array.
263
+ $(P $(I Expression)`[]` is shorthand for a slice of the entire array.
254
264
)
255
265
256
266
$(P Slicing
You can’t perform that action at this time.
0 commit comments