Zig arrays and slices
In the Zig programming language, arrays and slices are basic structures used to store and manipulate a set of data of the same type.
- arrays: Used to store a fixed-size set of data of the same type, the size is specified at definition time and stored on the stack.
- Slicing: Used to reference a part of an array or other contiguous memory area, the size can be adjusted dynamically, more flexible, and usually references heap memory.
Array:
- An array is a fixed-length sequence whose size is determined at compile time.
- The type of an array is
[T], whereTis the type of elements in the array. - The memory of an array is contiguous, which gives them a performance advantage, especially when processing large amounts of data.
Slice:
- A slice is a dynamically sized sequence that allows its size to change at runtime.
- The type of a slice is
[]T, whereTis the type of elements in the slice. - A slice is actually a reference to an array, which contains a pointer to the array and the length of the slice.
- Slices can handle data more flexibly because they can be easily shared and passed between different arrays.
arrays
Arrays are fixed-size, stored on the stack, and their element types must be the same.
You can use the [] syntax to define an array and specify its size.
The array type is defined as [T; N], where T is the type of the array elements and N is the length of the array.
Array elements can be accessed by index, and the index starts from 0.
Syntax format
const arrayName: [size]ElementType = [size]ElementType{element1, element2, ...};
Parameter Description:
arrayName: The name of the array.size: The size of the array (number of elements), which is a compile-time constant.ElementType: The type of elements in the array.element1, element2, ...: The elements in the array.
An array is a fixed-size contiguous memory block whose size is determined at compile time:
var myArray: [10]u8 = [10]u8{0} ** 10; // 定义并初始化一个大小为 10 的 u8 类型数组
Example
const
std
=
@import
(
"std"
)
;
pub fn main
(
)
void
{
// Define an array containing 5 elements of type i32
const
arr
:
[
5
]
i32
=
[
5
]
i32
{
1
,
2
,
3
,
4
,
5
}
;
// Access array elements by index
std.
debug
.
(
"First element: {}\n"
, .
{
arr
[
0
]
}
)
;
std.
debug
.
(
"Third element: {}\n"
, .
{
arr
[
2
]
}
)
;
// The size of the array is fixed
const
size
:
usize
=
arr.
len
;
std.
debug
.
(
"Array size: {}\n"
, .
{
size
}
)
;
}
The compilation output result is:
First element: 1
Third element: 3
Array size: 5
Iterate over the array
You can use a for loop to iterate over the elements of the array.
Example
const
std
=
@import
(
"std"
)
;
pub fn main
(
)
void
{
const
arr
:
[
5
]
i32
=
[
5
]
i32
{
1
,
2
,
3
,
4
,
5
}
;
var index
:
usize
=
0
;
// Iterate over the array
for
(
arr
)
|
item
|
{
std.
debug
.
(
"Index: {}, Item: {}\n"
, .
{
index, item
}
)
;
index
=
1
;
}
}
The compilation output result is:
Index: 0, Item: 1
Index: 1, Item: 2
Index: 2, Item: 3
Index: 3, Item: 4
Index: 4, Item: 5
Slice
A slice is a reference to a part of an array or other contiguous memory area.
A slice can be dynamically resized and is more flexible than an array, but its elements are stored on the heap.
Definition and initialization
A slice is a reference to a part of an array or other contiguous memory area.
A slice is dynamic and can change its size, usually used to represent a part of an array or a dynamically allocated memory block.
Syntax format
const sliceName: []ElementType = array[start..end];
Parameter Description:
sliceName: The name of the slice.ElementType: The type of elements in the slice.array[start..end]: from thearrayExtract a sub-slice from it,startandendis the index.
A slice can be created from a subset of an array, or through a pointer and length:
var myArray: [10]u8 = ...; // 假设已经初始化
var mySlice = myArray[2..7]; // 创建一个切片,包含索引2到6的元素
// Or use pointers and length
var mySlicePtr = myArray[2..]; // 创建一个切片,从索引2开始到数组末尾
Slices provide some built-in methods to manipulate slices, for example:
len: Get the length of the slice.ptr: Get the pointer of the slice.capacity: Get the capacity of the slice, that is, the maximum length of the array part it can reference.
Example
const
std
=
@import
(
"std"
)
;
pub fn main
(
)
void
{
var arr
:
[
5
]
i32
=
[
5
]
i32
{
1
,
2
,
3
,
4
,
5
}
;
// Create a slice from an array
const
slice
:
[
]
i32
=
arr
[
1
..
4
]
;
// Access slice elements by index
std.
debug
.
(
"First element of slice: {}\n"
, .
{
slice
[
0
]
}
)
;
std.
debug
.
(
"Second element of slice: {}\n"
, .
{
slice
[
1
]
}
)
;
// The length of the slice
const
length
:
usize
=
slice.
len
;
std.
debug
.
(
"Slice length: {}\n"
, .
{
length
}
)
;
}
The compilation output result is:
First element of slice: 2
Second element of slice: 3
Slice length: 3
Iterate over the slice
Similar to arrays, you can use a for loop to iterate over a slice.
Example
const
std
=
@import
(
"std"
)
;
pub fn main
(
)
void
{
var arr
:
[
5
]
i32
=
[
5
]
i32
{
1
,
2
,
3
,
4
,
5
}
;
const
slice
:
[
]
i32
=
arr
[
1
..
4
]
;
var index
:
usize
=
1
;
// Iterate over the slice
for
(
slice
)
|
item
|
{
std.
debug
.
(
"Index: {}, Item: {}\n"
, .
{
index, item
}
)
;
index
=
1
;
}
}
The compilation output result is:
Index: 1, Item: 2
Index: 2, Item: 3
Index: 3, Item: 4
Differences between arrays and slices
- Size: The size of an array is fixed and determined at definition time; the size of a slice can be adjusted dynamically.
- Storage location: Arrays are usually stored on the stack, while the memory referenced by a slice can be on the heap.
- Flexibility: Slices are more flexible and can reference a part of an array or dynamically allocated memory.
| Feature | arrays | Slicing |
|---|---|---|
| Size | Fixed, determined at compile time | Dynamic, can change size |
| Element type | are the same | are the same |
| Memory location | Usually on the stack (local variables) | The referenced memory may be on the heap or stack |
| Access the | Through index | Through the start and end index of the slice |
| creation | Direct definition | Create from an array or other slices |
In the following example, the printArray function accepts a fixed-size array as a parameter, while the printSlice function accepts a slice as a parameter. Through these functions, you can see the differences between arrays and slices in terms of passing and usage.
Example
const
std
=
@import
(
"std"
)
;
fn printArray
(
arr
:
[
5
]
i32
)
void
{
for
(
arr
)
|
item
|
{
std.
debug
.
(
"Array item: {}\n"
, .
{
item
}
)
;
}
}
fn printSlice
(
slice
:
[
]
const
i32
)
void
{
for
(
slice
)
|
item
|
{
std.
debug
.
(
"Slice item: {}\n"
, .
{
item
}
)
;
}
}
pub fn main
(
)
void
{
const
arr
:
[
5
]
i32
=
[
5
]
i32
{
1
,
2
,
3
,
4
,
5
}
;
const
slice
:
[
]
const
i32
=
arr
[
1
..
4
]
;
printArray
(
arr
)
;
printSlice
(
slice
)
;
}
The compilation output result is:
Array item: 1
Array item: 2
Array item: 3
Array item: 4
Array item: 5
Slice item: 2
Slice item: 3
Slice item: 4