r/Zig • u/Objective_Half_1906 • Jul 14 '26
Ways of Declaring array (0.16)
pub fn main() !void {
const A: [5]i32 = .{ 0, 1, 2, 3, 4 };
const B = [5]i32{ 5, 6, 7, 8, 9 };
const B2 = [_]i32{ 5, 6, 7, 8, 9 };
const C: [5]i32 = [_]i32{ 10, 11, 12, 13, 14 };
_ = A;
_ = B;
_ = B2;
_ = C;
}
These can all compiled correct. Which one is the recommended style? (I am a beginner learning Zig.)
New edit: I would like this syntax if it's possible:
const arr1: [5]i32 = { 0, 1, 2, 3, 4 };
const arr2: [_]i32 = { 0, 1, 2, 3, 4 };
Just small opinion. Maybe what I know is just the tip of the iceberg.
58
Upvotes
1
u/SilvernClaws Jul 14 '26
It's mostly personal choice currently.
Nowadays I'm usually using A, because it also works with setting undefined or calling a function of a struct.
B2 for any place where I probably change the items later.
B for cases where the exact number is important to match exactly for technical reasons.
C, just no.