Search code examples
stringmultilinestring-literalszig

How to declare a multiline string in Zig?


How can I write a string that spans over multiple lines in Zig?

For example:

var str = `hello
second line
world
fourth line
`

Solution

  • Zig has this weird but cool syntax with multiple lines starting with \\ and the ; in the next line.

    Here's a singleton:

    const str =
        \\hello
        \\second line
        \\world
        \\fourth line
    ;
    

    Here is an array of 2 multiline strings:

    const array_of_multiline_strings = [_][]const u8{
        \\hello
        \\world
        ,
        \\goodbye
        \\world
    };