#Cast []const u8 to [:0]const u8

1 messages · Page 1 of 1 (latest)

violet sandal
#

How can I Cast []const u8 to [:0]const u8?

is @as([:0]const u8, @ptrCast(x)) the correct way?

dapper lagoon
#

In that case you can do
slice[0..slice.len - 1 :0]
The :0 means the element immediately after that is the sentinel value (0)

violet sandal
#

I have a string I was concatenating and passing to a raylib function which takes a [:0]const u8
so i used the ptrcast and needed to add ++ ""; at the end of my string.

const cwd: []const u8 = std.fs.path.dirname(current_dir()).?;
const asset_dir: []const u8 = cwd ++ "/../assets/";

pub fn loadAssets(comptime asset_name: []const u8) rl.Texture2D {
    const asset: []const u8 = asset_dir ++ asset_name ++ "";
    return rl.loadTexture(@as([:0]const u8, @ptrCast(asset)));
}```

which seems to work and loads texture correctly
dapper lagoon
#

Yeah thats one way to do it

#

Its kind of a hack but it works in comptime

#

Also you shouldnt need that @as