Zig NEWS

Discussion on: When a var really isn't a var (ft. String Literals)

Collapse
yinameah profile image
Yinameah

Despite how much I enjoy discovering Zig, I do have trouble wrapping my head around certain concepts, strings literals being one of them.

I just spend some serious time trying to pass a string literal as a function argument. Despite this great article (zig.news/kristoff/what-s-a-string-...), which almost gives the answer, I kept banging my head to the wall.

I finally clicked after reading the post above, so I figure I could as well write it down, maybe it will help others :

pub fn main() void {
    functionWithStringLitteral("String litteral as argument");
}

fn functionWithStringLitteral(text: []const u8) void {
    std.debug.print("{s}", .{text});
}
Enter fullscreen mode Exit fullscreen mode

In other word, you want to pass a slice of immutable u8 values