typeover
curriculum

Curriculum Types Arrays vs slices exercise 1 · mcq

Arrays vs slices

TypeScript writes const arr: number[] = [10, 20, 30];. Zig has two shapes for fixed-length array initialization: the TYPE-PREFIX form ([3]i32{ ... } — type-name first, then the initializer) and the annotation-with-anonymous-literal form (exercise 06's const arr: [3]i32 = .{ ... };). The type-prefix form works WITHOUT a binding-side annotation — useful when passing an array directly to a function or storing it as a struct field. Pick the type-prefix shape.

TypeScript reference
Pick the idiomatic Go translation

About this theme

Zig has two collection shapes where TypeScript has one. [N]T is a fixed-length array; the length N is part of the type, known at compile time. []T is a slice: a pointer plus a runtime length. Pass an array to a function and you pass a copy; pass a slice (built via arr[0..]) and you pass a view. Module 3 builds on this.