In the examples file for my gui library, I want to declare a global mutable slice ([]u8) to use in a text entry field that starts filled with some example text. Here’s the best I’ve come up with:
var mutable = "abc".*;
var mutable_slice = &mutable; // mutable_slice is []u8
In short, you cannot make a literal mutable. The thing that assigns from a literal (the copy) can be mutable but the literal itself is not. Because of that, you’re basically going to initialize some memory (like you do on the first line) and then grab a slice to that memory (second line).
If you point directly at the literal, you need to have a constant pointer (non-mutable).
I thought maybe there was some clever way to use a comptime block that I hadn’t been able to figure out, but sounds like no. Your explanation is very good, and I had not considered making the mutable slice const. Thank you very much!
I get this error (same error for me on 0.13): % ../../apps/zig-macos-aarch64-0.14.0-dev.1550+4fba7336a/zig run test.zig test.zig:3:25: error: global variable contains reference to comptime var const mut_slice = init: {