DEV Community

Dayvster 🌊
Dayvster 🌊

Posted on • Originally published at dayvster.com

Building Blocks Of Zig: slices

What are Zig Slices?

Slices are a way to represent a contiguous sequence of elements in Zig. They are essentially a single item pointer and a usize length. Slice types are defined as []T where T is the type of the elements in the slice. Zig slices are used to represent arrays, string and other data structures that are contiguous in memory and can be accessed in a linear fashion.

How Are Zig Slices Different From Arrays?

Both single item arrays and slices in zig are fairly similar. You can get their length, iterate over them, and access their elements. In fact the Zig compiler will automatically convert a slice with an "unknown" length to a single item array pointer for us at compile time if we do not specify a predetermined fixed length for the slice that can be used during runtime.

The main difference between slices and arrays in zig is that the slices length has to be known at runtime while the arrays length is part of the type and known at compile time.

How To Create A Slice In Zig

To create a slice in Zig you can use the [] syntax. Here is an example of creating a slice of integers:

const std = @import("std");

pub fn main() void {
    // this array could be generated mathematically or a result of a function
    var arr = [_]i32{1,1,2,3,5,8,13,21,34,55}; 
    // create a slice of the first 5 elements of the array
    var slice = arr[0..5]; 
}
Enter fullscreen mode Exit fullscreen mode

What we have done there is created a slice out of the first 5 elements of the array. The slice is a pointer to the first element of the array and a length of 5. The array may be subject to change in length but the slice will always point to the first 5 elements of the array and always have a fixed length of 5.

How To Access Elements In A Slice

You can access elements in a slice using the [] syntax. Here is an example of accessing the first element in a slice:

const std = @import("std");

pub fn main() void {
    var arr = [_]i32{1,1,2,3,5,8,13,21,34,55}; 
    var slice = arr[0..5]; 
    const firstElement = slice[0];
}
Enter fullscreen mode Exit fullscreen mode

Just like with arrays, you can access elements in a slice using the [] syntax. The first element in the slice is accessed by using slice[0].

How To Iterate Over A Slice

You can iterate over a slice using a for loop. Here is an example of iterating over a slice of integers:

const std = @import("std");

pub fn main() void {
    var arr = [_]i32{1,1,2,3,5,8,13,21,34,55}; 
    var slice = arr[0..5]; 
    for (slice) |element| {
        std.debug.print("{d}\n", .{element});
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example we are simply iterating over each element in the slice and printing it out to the console. Much like you would with an array.

Conclusion

Slices are a powerful feature in Zig that allow you to work with contiguous sequences of elements. They are similar to arrays but have a few key differences. Slices are a pointer to the first element of the sequence and a length. You can create slices using the [] syntax and access elements in a slice using the [] syntax. You can also iterate over a slice using a for loop. Slices are a fundamental building block in Zig and are used to represent arrays, strings and other data structures that are contiguous in memory and have a fixed length that is known at runtime.

Top comments (0)