Values

    Shell

    1. $ ./values
    2. 1 + 1 = 2
    3. 7.0 / 3.0 = 2.33333325e+00
    4. false
    5. true
    6. false
    7. optional 1
    8. type: ?[]const u8
    9. value: null
    10. optional 2
    11. type: ?[]const u8
    12. value: hi
    13. error union 1
    14. type: anyerror!i32
    15. value: error.ArgNotFound
    16. error union 2
    17. type: anyerror!i32
    18. value: 1234

    In addition to the integer types above, arbitrary bit-width integers can be referenced by using an identifier of i or u followed by digits. For example, the identifier i7 refers to a signed 7-bit integer. The maximum allowed bit-width of an integer type is 65535.

    See also:

    See also:

    String literals are constant single-item to null-terminated byte arrays. The type of string literals encodes both the length, and the fact that they are null-terminated, and thus they can be coerced to both and Null-Terminated Pointers. Dereferencing string literals converts them to .

    The encoding of a string in Zig is de-facto assumed to be UTF-8. Because Zig source code is UTF-8 encoded, any non-ASCII bytes appearing within a string literal in source code carry their UTF-8 meaning into the content of the string in the Zig program; the bytes are not modified by the compiler. However, it is possible to embed non-UTF-8 bytes into a string literal using \xNN notation.

    Unicode code point literals have type comptime_int, the same as . All Escape Sequences are valid in both string literals and Unicode code point literals.

    In many other programming languages, a Unicode code point literal is called a “character literal”. However, there is in recent versions of the Unicode specification (as of Unicode 13.0). In Zig, a Unicode code point literal corresponds to the Unicode definition of a code point.

    string_literals.zig

    1. const print = @import("std").debug.print;
    2. const mem = @import("std").mem; // will be used to compare bytes
    3. pub fn main() void {
    4. const bytes = "hello";
    5. print("{s}\n", .{@typeName(@TypeOf(bytes))}); // *const [5:0]u8
    6. print("{d}\n", .{bytes.len}); // 5
    7. print("{c}\n", .{bytes[1]}); // 'e'
    8. print("{d}\n", .{bytes[5]}); // 0
    9. print("{}\n", .{'e' == '\x65'}); // true
    10. print("{d}\n", .{'\u{1f4a9}'}); // 128169
    11. print("{d}\n", .{'💯'}); // 128175
    12. print("{}\n", .{mem.eql(u8, "hello", "h\x65llo")}); // true
    13. print("0x{x}\n", .{"\xff"[0]}); // non-UTF-8 strings are possible with \xNN notation.
    14. print("{u}\n", .{'⚡'});
    15. }
    1. $ zig build-exe string_literals.zig
    2. $ ./string_literals
    3. *const [5:0]u8
    4. 5
    5. e
    6. 0
    7. true
    8. 128169
    9. true
    10. 0xff

    See also:

    Note that the maximum valid Unicode point is 0x10ffff.

    Multiline string literals have no escapes and can span across multiple lines. To start a multiline string literal, use the \\ token. Just like a comment, the string literal goes until the end of the line. The end of the line is not included in the string literal. However, if the next line begins with \\ then a newline is appended and the string literal continues.

    multiline_string_literals.zig

    See also:

    Use the const keyword to assign a value to an identifier:

    constant_identifier_cannot_change.zig

    1. const x = 1234;
    2. fn foo() void {
    3. const y = 5678;
    4. // Once assigned, an identifier cannot be changed.
    5. y += 1;
    6. }
    7. pub fn main() void {
    8. foo();
    9. }

    Shell

    1. $ zig build-exe constant_identifier_cannot_change.zig
    2. ./docgen_tmp/constant_identifier_cannot_change.zig:8:7: error: cannot assign to constant
    3. y += 1;
    4. ^

    const applies to all of the bytes that the identifier immediately addresses. have their own const-ness.

    mutable_var.zig

    1. const print = @import("std").debug.print;
    2. pub fn main() void {
    3. var y: i32 = 5678;
    4. y += 1;
    5. print("{d}", .{y});
    6. }

    Shell

    Variables must be initialized:

    var_must_be_initialized.zig

    1. pub fn main() void {
    2. var x: i32;
    3. x = 1;
    4. }

    Shell

    1. $ zig build-exe var_must_be_initialized.zig
    2. docgen_tmp/var_must_be_initialized.zig:2:5: error: variables must be initialized
    3. var x: i32;
    4. ^

    Use undefined to leave variables uninitialized:

    assign_undefined.zig

    1. const print = @import("std").debug.print;
    2. pub fn main() void {
    3. var x: i32 = undefined;
    4. x = 1;
    5. }

    Shell

    undefined can be coerced to any type. Once this happens, it is no longer possible to detect that the value is undefined. undefined means the value could be anything, even something that is nonsense according to the type. Translated into English, means “Not a meaningful value. Using this value would be a bug. The value will be unused, or overwritten before being used.”