I write follow code:
pub fn main() !void {
const array = []u8{1,2};
array[1] = 3;
}
And it is not work:
xxx/h.zig: error: cannot assign to constant
array[1] = 3;
I want to know whether the keyword const is apply to all the embed types, but I can not find the document anywhere.
It looks like that the const defined is applied to all the embed types.
I try to look to const in the index, I can not find it.
I try to search "const" in the html, but i get a lot of results, there should be titles called “keyword const”, “keyword var” ... so I can search "keyword const"
Maybe retitle this to "Document const Semantics"
A search for "cannot assign to constant" leads you to the correct section: https://ziglang.org/documentation/master/#Assignment
I made some modifications so that "const keyword" and "var keyword" would turn up results.
Please tell me if you can suggest additional content that would have resolved this question for you.
Digging up an old issue, apologies if this isn't the right place for it!
Although there are a few mentions of const in the docs, it would be super useful to enumerate the possible uses of it - e.g. constant value, constant pointer address v.s. constant data behind a variable pointer, and similarly for arrays/slices/structs & their content. This is especially confusing coming from C since type declarations and their * is on the opposite side of the variable name.
It might feel obvious for people who already know Zig intimately, but something like a cheatsheet of example types and what they mean would be incredibly useful!
Most helpful comment
Digging up an old issue, apologies if this isn't the right place for it!
Although there are a few mentions of
constin the docs, it would be super useful to enumerate the possible uses of it - e.g. constant value, constant pointer address v.s. constant data behind a variable pointer, and similarly for arrays/slices/structs & their content. This is especially confusing coming from C since type declarations and their*is on the opposite side of the variable name.It might feel obvious for people who already know Zig intimately, but something like a cheatsheet of example types and what they mean would be incredibly useful!