In Zig, const
applies to the next thing in the declaration.
So [_][] u8
is an array of u8
slices, while [_][] const u8
is an array of slices of const u8
. Your string literals are *const [_:0]u8
(pointer to null-terminated array of u8; that's where the *const [6:0] u8
in your error message is from), which Zig can coerce to slices of const u8
.
Some examples and how mutable they are:
[_][]u8
- Everything is mutable.
var move: [3][]u8 = undefined;
var ziga: [4]u8 = [_]u8{ 'z', 'i', 'g', 's' };
const zigs: []u8 = ziga[0..];
move[0] = zigs;
move[0][1] = 'a';
[_][] const u8
- The slice is mutable, but things in it are not.
var belong_to_us = [_][]const u8{ "all", "your", "base", "are" };
var bomb = [_][]const u8{ "someone", "set", "up", "us" };
belong_to_us = bomb;
but
bomb[0][0] = 'x'; // error: cannot assign to constant
const [_][] const u8
- The whole thing is immutable.
const signal: [3][]const u8 = [_][]const u8{ "we", "get", "signal" };
const go: [3][]const u8 = [_][]const u8{ "move", "every", "zig" };
signal = go; // error: cannot assign to constant
However,
const [_][]u8
- This is a const array of slices of u8.
var what: [4]u8 = [_]u8{ 'w', 'h', 'a', 't' };
const signal: [3][]u8 = [_][]u8{ zigs, what[0..], zigs };
signal[0][1] = 'f'; // Legal!
signal[1] = zigs; // error: cannot assign to constant
That last one is a constant array of slices of mutable u8
.