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

SyntaxRelevant TypesDescriptionExample
  1. a + b
  2. a += b
Addition.
  1. 2 + 5 == 7
  1. a +% b
  2. a +%= b
Wrapping Addition.
  1. @as(u32, std.math.maxInt(u32)) +% 1 == 0
  1. a - b
  2. a -= b
Subtraction.
  1. 2 - 5 == -3
  1. a -% b
  2. a -%= b
Wrapping Subtraction.
  1. @as(u32, 0) -% 1 == std.math.maxInt(u32)
  1. -a
Negation.
  1. -1 == 0 - 1
  1. -%a
Wrapping Negation.
  • Guaranteed to have twos-complement wrapping behavior.
  1. -%@as(i32, std.math.minInt(i32)) == std.math.minInt(i32)
  1. a b
  2. a = b
Multiplication.
  1. 2 5 == 10
  1. a % b
  2. a %= b
Wrapping Multiplication.
  1. @as(u8, 200) % 2 == 144
  1. a / b
  2. a /= b
Division.
  1. 10 / 5 == 2
  1. a % b
  2. a %= b
Remainder Division.
  1. 10 % 3 == 1
  1. a << b
  2. a <<= b
Bit Shift Left.
  1. 1 << 8 == 256
  1. a >> b
  2. a >>= b
Bit Shift Right.
  1. 10 >> 1 == 5
  1. a & b
  2. a &= b
Bitwise AND.
  1. 0b011 & 0b101 == 0b001
  1. a | b
  2. a |= b
Bitwise OR.
  1. 0b010 | 0b100 == 0b110
  1. a ^ b
  2. a ^= b
Bitwise XOR.
  1. 0b011 ^ 0b101 == 0b110
  1. ~a
Bitwise NOT.
  1. ~@as(u8, 0b10101111) == 0b01010000
  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
  1. a.?
Equivalent to:
  1. a orelse unreachable
  1. const value: ?u32 = 5678;
  2. value.? == 5678
  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
  1. a and b
If a is false, returns false without evaluating b. Otherwise, returns b.
  1. (false and true) == false
  1. a or b
If a is true, returns true without evaluating b. Otherwise, returns b.
  1. false or true == true
  1. !a
Boolean NOT.
  1. !false == true
  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
  1. a == null
Returns true if a is null, otherwise returns false.
  1. const value: ?u32 = null;
  2. value == null
  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
  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
  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
  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>
  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
  1. a ++ b
Array concatenation.
  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})
  1. a b
Array multiplication.
  1. const mem = @import(“std”).mem;
  2. const pattern = ab 3;
  3. mem.eql(u8, pattern, ababab”)
  1. a.
Pointer dereference.
  1. const x: u32 = 1234;
  2. const ptr = &x;
  3. ptr. == 1234
  1. &a
All typesAddress of.
  1. const x: u32 = 1234;
  2. const ptr = &x;
  3. ptr.* == 1234
  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
  2. a!b
  3. x{} x.* x.?
  4. !x -x -%x ~x &x ?x
  5. ! * / % ** *% ||
  6. + - ++ +% -%
  7. << >>
  8. & ^ |
  9. == != < > <= >=
  10. and
  11. or
  12. orelse catch
  13. = *= /= %= += -= <<= >>= &= ^= |=