C3 Tutorial
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Slices

  • A subarray / slice has the syntax int[] subarray.
  • To create a subarray you can either use a range from an array : subarray = list[3..5] or create it from a literal: int[] subarray = { 1, 3, 9, 16 };
  • There are some important shortcuts when slicing:
// Get the entire array as a slice:
int[] foo1 = list[..]; // 1. slicing the entire
int[] foo2 = &list; // 2. convert from an array pointer
// Get the first 3 elements
int[] foo3 = list[0..2];
int[] foo4 = list[..2];
int[] foo5 = list[0:3];
int[] foo5 = list[:3];
// Get the last 2 elements (of an array with 6 elements):
int[] foo6 = list[4..5];
int[] foo7 = list[4..];
int[] foo8 = list[^2..]; // ^2 means "len - 2"
int[] foo8 = list[^2..^1]; 
int[] foo9 = list[^2:2];