Values
$ ./values
1 + 1 = 2
7.0 / 3.0 = 2.33333325e+00
false
true
false
optional 1
type: ?[]const u8
value: null
optional 2
type: ?[]const u8
value: hi
error union 1
type: anyerror!i32
value: error.ArgNotFound
error union 2
type: anyerror!i32
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 single-item constant to null-terminated UTF-8 encoded 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 .
Character literals have type comptime_int
, the same as Integer Literals. All are valid in both string literals and character literals.
test.zig
const expect = @import("std").testing.expect;
const mem = @import("std").mem;
test "string literals" {
const bytes = "hello";
expect(@TypeOf(bytes) == *const [5:0]u8);
expect(bytes.len == 5);
expect(bytes[1] == 'e');
expect(bytes[5] == 0);
expect('e' == '\x65');
expect('\u{1f4a9}' == 128169);
expect('💯' == 128175);
expect(mem.eql(u8, "hello", "h\x65llo"));
}
$ zig test test.zig
1/1 test "string literals"... OK
All 1 tests passed.
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.
See also:
Use the const
keyword to assign a value to an identifier:
test.zig
const x = 1234;
fn foo() void {
// It works at global scope as well as inside functions.
const y = 5678;
// Once assigned, an identifier cannot be changed.
}
test "assignment" {
foo();
}
$ zig test test.zig
./docgen_tmp/test.zig:8:7: error: cannot assign to constant
y += 1;
^
const
applies to all of the bytes that the identifier immediately addresses. Pointers have their own const-ness.
test.zig
const expect = @import("std").testing.expect;
test "var" {
var y: i32 = 5678;
y += 1;
expect(y == 5679);
}
Variables must be initialized:
test.zig
test "initialization" {
var x: i32;
x = 1;
}
$ zig test test.zig
./docgen_tmp/test.zig:2:5: error: variables must be initialized
var x: i32;
^
Use undefined
to leave variables uninitialized:
test.zig
const expect = @import("std").testing.expect;
test "init with undefined" {
var x: i32 = undefined;
x = 1;
}
undefined
can be 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, undefined
means “Not a meaningful value. Using this value would be a bug. The value will be unused, or overwritten before being used.”