Passing a slice of strings to a function

Hi!
I’m trying to pass a slice of strings to a function:

const std = @import("std");
fn loopOverSlice(strSlice: [][]const u8) void {
    for(strSlice) | str | {
        std.debug.print("{s}", .{str});
    }
}

test "str slice text" {
    const list = [_][]const u8{"hello", "world"};
    loopOverSlice(list);
}

If I run this, I get the following error:

src/main.zig:10:19: error: array literal requires address-of operator (&) to coerce to slice type '[][]const u8'
    loopOverSlice(list);
                  ^~~~

If I add the & when passing the list to the function, I get the following error:

src/main.zig:10:19: error: expected type '[][]const u8', found '*const [2][]const u8'
    loopOverSlice(&list);
                  ^~~~~
src/main.zig:10:19: note: cast discards const qualifier
src/main.zig:2:28: note: parameter type declared here
fn loopOverSlice(strSlice: [][]const u8) void {
fn loopOverSlice(strSlice: []const []const u8) void {
1 Like