2 ms·
> Imagine you are writing performance sensitive code. You want to get a substring from a string, one that is not going to live outside your hot loop. Zig uses
by samatman 2y ago
> Imagine you are writing performance sensitive code. You want to get a substring from a string, one that is not going to live outside your hot loop.
Zig uses slices for this (and everything else except interop): a pointer and a length, with some nicely ergonomic syntax for making another one, like `slice[start..][0..length]`.
When you're building strings then you have an ArrayList, which keeps a capacity around, and maybe a pointer to an Allocator depending on what you want. It's trivial to get the slice out of this when you need it.
Doing anything useful with a string requires knowing where it is (the pointer) and how much of it you have (the length) so keeping them together in one fat pointer is just good sense. It's remarkable how much easier this is to work with than C strings.