Operators

There is no operator overloading. When you see an operator in Zig, you know that it is doing something from this table, and nothing else.

Table of Operators

NameSyntaxTypesRemarksExample
Addition
  1. a + b
  2. a += b
  1. 2 + 5 == 7
Wrapping Addition
  1. a +% b
  2. a +%= b
  1. @as(u32, 0xffffffff) +% 1 == 0
Saturating Addition
  1. a +| b
  2. a +|= b
  1. @as(u8, 255) +| 1 == @as(u8, 255)
Subtraction
  1. a - b
  2. a -= b
  1. 2 - 5 == -3
Wrapping Subtraction
  1. a -% b
  2. a -%= b
  1. @as(u8, 0) -% 1 == 255
Saturating Subtraction
  1. a -| b
  2. a -|= b
  1. @as(u32, 0) -| 1 == 0
Negation
  1. -a
  1. -1 == 0 - 1
Wrapping Negation
  1. -%a
  • Twos-complement wrapping behavior.
  1. -%@as(i8, -128) == -128
Multiplication
  1. a b
  2. a = b
  1. 2 5 == 10
Wrapping Multiplication
  1. a % b
  2. a %= b
  1. @as(u8, 200) % 2 == 144
Saturating Multiplication
  1. a | b
  2. a |= b
  1. @as(u8, 200) | 2 == 255
Division
  1. a / b
  2. a /= b
  1. 10 / 5 == 2
Remainder Division
  1. a % b
  2. a %= b
  1. 10 % 3 == 1
Bit Shift Left
  1. a << b
  2. a <<= b
  1. 0b1 << 8 == 0b100000000
Saturating Bit Shift Left
  1. a <<| b
  2. a <<|= b
  1. @as(u8, 1) <<| 8 == 255
Bit Shift Right
  1. a >> b
  2. a >>= b
  • Moves all bits to the right, inserting zeroes at the most-significant bit.
  • b must be comptime-known or have a type with log2 number of bits as a.
  • See also @shrExact.
  1. 0b1010 >> 1 == 0b101
Bitwise And
  1. a & b
  2. a &= b
  1. 0b011 & 0b101 == 0b001
Bitwise Or
  1. a | b
  2. a |= b
  1. 0b010 | 0b100 == 0b110
Bitwise Xor
  1. a ^ b
  2. a ^= b
  1. 0b011 ^ 0b101 == 0b110
Bitwise Not
  1. ~a
  1. ~@as(u8, 0b10101111) == 0b01010000
Defaulting Optional Unwrap
  1. a orelse b
If a is null, returns b (“default value”), otherwise returns the unwrapped value of a. Note that b may be a value of type noreturn.
  1. const value: ?u32 = null;
  2. const unwrapped = value orelse 1234;
  3. unwrapped == 1234
Optional Unwrap
  1. a.?
Equivalent to:
  1. a orelse unreachable
  1. const value: ?u32 = 5678;
  2. value.? == 5678
Defaulting Error Unwrap
  1. a catch b
  2. a catch |err| b
If a is an error, returns b (“default value”), otherwise returns the unwrapped value of a. Note that b may be a value of type noreturn. err is the error and is in scope of the expression b.
  1. const value: anyerror!u32 = error.Broken;
  2. const unwrapped = value catch 1234;
  3. unwrapped == 1234
Logical And
  1. a and b
If a is false, returns false without evaluating b. Otherwise, returns b.
  1. (false and true) == false
Logical Or
  1. a or b
If a is true, returns true without evaluating b. Otherwise, returns b.
  1. (false or true) == true
Boolean Not
  1. !a
  1. !false == true
Equality
  1. a == b
Returns true if a and b are equal, otherwise returns false. Invokes Peer Type Resolution for the operands.
  1. (1 == 1) == true
Null Check
  1. a == null
Returns true if a is null, otherwise returns false.
  1. const value: ?u32 = null;
  2. (value == null) == true
Inequality
  1. a != b
Returns false if a and b are equal, otherwise returns true. Invokes Peer Type Resolution for the operands.
  1. (1 != 1) == false
Non-Null Check
  1. a != null
Returns false if a is null, otherwise returns true.
  1. const value: ?u32 = null;
  2. (value != null) == false
Greater Than
  1. a > b
Returns true if a is greater than b, otherwise returns false. Invokes Peer Type Resolution for the operands.
  1. (2 > 1) == true
Greater or Equal
  1. a >= b
Returns true if a is greater than or equal to b, otherwise returns false. Invokes Peer Type Resolution for the operands.
  1. (2 >= 1) == true
Less Than
  1. a < b
Returns true if a is less than b, otherwise returns false. Invokes Peer Type Resolution for the operands.
  1. (1 < 2) == true
Lesser or Equal
  1. a <= b
Returns true if a is less than or equal to b, otherwise returns false. Invokes Peer Type Resolution for the operands.
  1. (1 <= 2) == true
Array Concatenation
  1. a ++ b
  1. const mem = @import(“std”).mem;
  2. const array1 = []u32{1,2};
  3. const array2 = []u32{3,4};
  4. const together = array1 ++ array2;
  5. mem.eql(u32, &together, &[_]u32{1,2,3,4})
Array Multiplication
  1. a b
  1. const mem = @import(“std”).mem;
  2. const pattern = ab 3;
  3. mem.eql(u8, pattern, ababab”)
Pointer Dereference
  1. a.
Pointer dereference.
  1. const x: u32 = 1234;
  2. const ptr = &x;
  3. ptr. == 1234
Address Of
  1. &a
All types
  1. const x: u32 = 1234;
  2. const ptr = &x;
  3. ptr. == 1234
Error Set Merge
  1. a || b
Merging Error Sets
  1. const A = error{One};
  2. const B = error{Two};
  3. (A || B) == error{One, Two}

Precedence

  1. x() x[] x.y x.* x.?
  2. a!b
  3. x{}
  4. !x -x -%x ~x &x ?x
  5. * / % ** *% *| ||
  6. + - ++ +% -% +| -|
  7. << >> <<|
  8. & ^ | orelse catch
  9. == != < > <= >=
  10. and
  11. or
  12. = *= *%= *|= /= %= += +%= +|= -= -%= -|= <<= <<|= >>= &= ^= |=