Slicing with array[start..end]
Carve sub-views without copying.
Slicing with array[start..end] is a free Zig Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Carving Out a View
Sometimes you want only part of an array. Slicing lets you grab a sub-range as a new slice without copying any elements. ✂️
The Range Syntax
Write the start and end inside brackets joined by two dots: array[start..end]. This produces a slice over those positions.
const part = data[1..3];End Is Exclusive
The end index is exclusive, so array[1..3] includes positions 1 and 2 but stops before 3. The result has a length of 2.
Slice to the End
Leave the end off to run all the way to the last element. data[2..] means everything from index 2 onward.
const tail = data[2..];Slice the Whole Thing
Using [0..] turns an entire array into a slice. It is the common way to hand a full array to slice-taking code.
const all = data[0..];No Copy Happens
Slicing only computes a new pointer and length. The elements stay shared with the original array, so it is fast and allocation-free.
Slicing a Slice
You can slice a slice just like an array. The new range is relative to the slice's own start, narrowing the view further.
const inner = part[0..1];Out-of-Range Is Caught
If start or end falls outside the data, safe builds panic at runtime. This stops you from reading memory you never owned.
Compile-Time Slices
When the bounds are known at compile time, the result can be a pointer to an array with a fixed length baked into its type.
Sentinel-Terminated Slices
Add a terminator with [start..end :0] to guarantee a trailing zero. This is handy when passing strings to C code.
const cstr = buf[0..n :0];Length from the Range
A slice's length is simply end minus start. So data[2..5] always yields a slice whose len is exactly 3.
const three = data[2..5]; // len == 3Quick Check
You have const data = [_]i32{ 0, 1, 2, 3, 4 }. What is the length of data[1..4]?
Recap
The array[start..end] syntax carves a sub-view: end is exclusive, omit it for the tail, nothing is copied, and bounds stay checked. 🎯
Frequently asked questions
Is the “Slicing with array[start..end]” lesson free?
Yes — the full text of “Slicing with array[start..end]” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.
What will I learn in “Slicing with array[start..end]”?
Carve sub-views without copying. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Zig Academy?
No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Slicing with array[start..end]” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Zig Academy lesson?
Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Fixed-Size Arrays
- Slices: A Pointer Plus a Length
- Slicing with array[start..end]
- Iterating and Mutating Slices