Skip to main content

Array Literals

The array's literals is represented in the form of ArrayType{list}:

//static array literal
[3]u8{1, 2, 3, 4, 5}
//the size of static array could be deduced
[_]u8{1, 3, 4, 6}

//multidimension static array literal
[_][_]i32{
{1, 2, 3},
{2, 1, 3},
{3 ,2 ,1}, //redundant comma is allowed
}

//dynamic array literal
[]i32{1, 4, 5, 4}

//map literal
[string]bool{ {"hello", true}, {"world", true} }

//set literal
[char]{'a', 'c', 'd'}

If the variable is already has a array type, you can use the anonymous array literals:

arr: [_]i8 = .{1, 3, 5, 7, 9};

If the variable's type is to be deduced, the anonymous array literals will be deduced to be a tuple:

tup := .{1, 'a', true, 2.0f};
assert(tup.[1] == 'a');
assert(tup::[2] == bool);