Writing end-of-string to a buffer

Hi Folks,

I have the following code:

const std = @import("std");
const mem = std.mem;
const testing = std.testing;

test "endofstring" {
    const buffer: []u8 = try testing.allocator.alloc(u8,16);
    defer testing.allocator.free(buffer);

    buffer[0] = 'F';
    buffer[1] = 'O';
    buffer[2] = 'O';

    try testing.expectEqualStrings("FOO", buffer);
}

The output of the test is:
====== expected this output: =========
FOO␃

======== instead found this: =========
FOO�������������␃

======================================

How can I add the end-of-string after “FOO” instead of at the end of the allocated buffer (without using something like std.Io.Writer).

In Zig, a string is a pair of a pointer and a length, there’s no end-of-string character. So you need something like this:

const foo: []u8 = buffer[0..3];
foo[0] = 'F';
foo[1] = 'O';
foo[2] = 'O';

try testing.expectEqualStrings("FOO", foo);

@sizeOf([]u8) is 2 * @sizeOf(usize).

Got it. So taking a slice set’s the length and where the string ends.