Mem.replace clearning out buffer

Hello everyone. I’m new to Zig and might not understand something around here.
I’m following learning Introduction Book to Zig, and I’m on this section which talks about std.mem.replace method. I’m confused, because my buffer is cleared out after running said method.

Code example:

{
    // Replace
    const s7: []const u8 = "Hello";
    var buf = [8]u8{ 1, 2, 3, 4, 5, 6, 7, 8 };
    const count = mem.replace(u8, s7, "el", "34", buf[0..5]);
    std.debug.print("N of replacements: {d}\n", .{count});
    std.debug.print("New string: {s}\n", .{buf});
}
// Expected output:
N of replacements: 1
New string: H34lo678

// Actual output:
N of replacements: 1
New string: H34lo

Could you please expalin why this is happening? Thanks.

The values in buf are not the ASCII encodings for the digits one through eight. They’re the values 1 to 8. These are control characters and not printable characters, which explains the result you’re seeing

The content of buf after mem.replace is [8]u8{ 'H', '3', '4', 'l', 'o', 6, 7, 8 } not [8]u8{ 'H', '3', '4', 'l', 'o', '6', '7', '8' }.

'6' != 6

6 Likes