Slice Conversion

Adapted from @dimdin’s post: Convert []const u8 to [*:0]const u8 - #2 by dimdin

Slice Types

  • [*:0]const u8 is a sentinel terminated pointer, a pointer to 0 terminated string of u8.
  • []const u8 is a slice, a pointer and a length.
  • [:0]const u8 is a 0 terminated sentinel slice, a pointer to 0 terminated string and a length.

Conversions

  • To convert from a zero terminated pointer to a slice use: std.mem.span .
  • To convert from a 0 terminated sentinel slice to a zero terminated pointer call .ptr
// "Hello" is a zero terminated array of u8
const hello: [:0]const u8 = "Hello";
const ptr: [*:0]const u8 = hello.ptr;
1 Like

Adapted from @dimdin’s post: Convert []const u8 to [*:0]const u8 - #2 by dimdin

This needs more examples and explanations.

I liked this article on strings in Zig. Maybe it’s a nice point to start?

1 Like

If you can, please edit the Doc and add your two cents!