Slices in strings

Strings have slices
const text: const u8
But why cannot the slice be a var? is it beacuse the slice is immutable or the string is?

To get a mutable slice, the underlying data must be mutable as well.

You can have a slice to non-const data, []u8.

I’m guessing you’re referring to string literals, they are slices to constant data []const u8 because all pointers to comptime known data point to constant data. This is so that data can live in the read only memory of the program.

That decision is because Zig wants all allocations to be explicit, even stack allocations. If you need to make a string literal mutate-able, you can copy it to a var

var str = "hello, world".*;

This will put the string in an array on the stack, which you can then get a pointer to it which you can mutate it through. But remember that you can’t (well shouldn’t) change the length.

If you to need deal with growing or shrinking length use an array list.