Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Zig arrays and slices

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.

Array:

Slice:


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:

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

.

print

(

"First element: {}\n"

, .

{

arr

[

0

]

}

)

;

    std.

debug

.

print

(

"Third element: {}\n"

, .

{

arr

[

2

]

}

)

;

   

// The size of the array is fixed

   

const

size

:

usize

=

arr.

len

;

    std.

debug

.

print

(

"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

.

print

(

"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:

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:

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

.

print

(

"First element of slice: {}\n"

, .

{

slice

[

0

]

}

)

;

    std.

debug

.

print

(

"Second element of slice: {}\n"

, .

{

slice

[

1

]

}

)

;

   

// The length of the slice

   

const

length

:

usize

=

slice.

len

;

    std.

debug

.

print

(

"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

.

print

(

"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

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

.

print

(

"Array item: {}\n"

, .

{

item

}

)

;

   

}

}

fn printSlice

(

slice

:

[

]

const

i32

)

void

{

   

for

(

slice

)

|

item

|

{

        std.

debug

.

print

(

"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