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

Syntax Relevant Types Description Example
  1. a + b
  2. a += b
- Integers - Floats Addition. - Can cause overflow for integers. - Invokes Peer Type Resolution for the operands. - See also @addWithOverflow.
  1. 2 + 5 == 7
  1. a +% b
  2. a +%= b
- Integers Wrapping Addition. - Guaranteed to have twos-complement wrapping behavior. - Invokes Peer Type Resolution for the operands. - See also @addWithOverflow.
  1. u32(std.math.maxInt(u32)) +% 1 == 0
  1. a - b
  2. a -= b
- Integers - Floats Subtraction. - Can cause overflow for integers. - Invokes Peer Type Resolution for the operands. - See also @subWithOverflow.
  1. 2 - 5 == -3
  1. a -% b
  2. a -%= b
- Integers Wrapping Subtraction. - Guaranteed to have twos-complement wrapping behavior. - Invokes Peer Type Resolution for the operands. - See also @subWithOverflow.
  1. u32(0) -% 1 == std.math.maxInt(u32)
  1. -a
- Integers - Floats Negation. - Can cause overflow for integers.
  1. -1 == 0 - 1
  1. -%a
- Integers Wrapping Negation. - Guaranteed to have twos-complement wrapping behavior.
  1. -%i32(std.math.minInt(i32)) == std.math.minInt(i32)
  1. a b
  2. a = b
- Integers - Floats Multiplication. - Can cause overflow for integers. - Invokes Peer Type Resolution for the operands. - See also @mulWithOverflow.
  1. 2 5 == 10
  1. a % b
  2. a %= b
- Integers Wrapping Multiplication. - Guaranteed to have twos-complement wrapping behavior. - Invokes Peer Type Resolution for the operands. - See also @mulWithOverflow.
  1. u8(200) % 2 == 144
  1. a / b
  2. a /= b
- Integers - Floats Division. - Can cause overflow for integers. - Can cause Division by Zero for integers. - Can cause Division by Zero for floats in FloatMode.Optimized Mode. - For non-compile-time-known signed integers, must use @divTrunc, @divFloor, or @divExact instead of /.
- Invokes Peer Type Resolution for the operands.
  1. 10 / 5 == 2
  1. a % b
  2. a %= b
- Integers - Floats Remainder Division. - Can cause Division by Zero for integers. - Can cause Division by Zero for floats in FloatMode.Optimized Mode. - For non-compile-time-known signed integers, must use @rem or @mod instead of %.
- Invokes Peer Type Resolution for the operands.
  1. 10 % 3 == 1
  1. a << b
  2. a <<= b
- Integers Bit Shift Left. - b must be comptime-known or have a type with log2 number of bits as a. - See also @shlExact. - See also @shlWithOverflow.
  1. 1 << 8 == 256
  1. a >> b
  2. a >>= b
- Integers Bit Shift Right. - b must be comptime-known or have a type with log2 number of bits as a. - See also @shrExact.
  1. 10 >> 1 == 5
  1. a & b
  2. a &= b
- Integers Bitwise AND. - Invokes Peer Type Resolution for the operands.
  1. 0b011 & 0b101 == 0b001
  1. a | b
  2. a |= b
- Integers Bitwise OR. - Invokes Peer Type Resolution for the operands.
  1. 0b010 | 0b100 == 0b110
  1. a ^ b
  2. a ^= b
- Integers Bitwise XOR. - Invokes Peer Type Resolution for the operands.
  1. 0b011 ^ 0b101 == 0b110
  1. ~a
- Integers Bitwise NOT.
  1. ~u8(0b0101111) == 0b1010000
  1. a orelse b
- Optionals 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.?
- Optionals Equivalent to:
  1. a orelse unreachable
  1. const value: ?u32 = 5678;
  2. value.? == 5678
  1. a catch b
  2. a catch |err| b
- Error Unions 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
- bool If a is false, returns false without evaluating b. Otherwise, returns b.
  1. false and true == false
  1. a or b
- bool If a is true, returns true without evaluating b. Otherwise, returns b.
  1. false or true == true
  1. !a
- bool Boolean NOT.
  1. !false == true
  1. a == b
- Integers - Floats - bool - type 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
- Optionals Returns true if a is null, otherwise returns false.
  1. const value: ?u32 = null;
  2. value == null
  1. a != b
- Integers - Floats - bool - type 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
- Integers - Floats 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
- Integers - Floats 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
- Integers - Floats 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
- Integers - Floats 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
- Arrays Array concatenation. - Only available when a and b are compile-time known.
  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
- Arrays Array multiplication. - Only available when a and b are compile-time known.
  1. const mem = @import("std").mem;
  2. const pattern = "ab" 3;
  3. mem.eql(u8, pattern, "ababab")
  1. a.
- Pointers Pointer dereference.
  1. const x: u32 = 1234;
  2. const ptr = &x;
  3. ptr. == 1234
  1. &a
All types Address of.
  1. const x: u32 = 1234;
  2. const ptr = &x;
  3. ptr.* == 1234
  1. a || b
- Error Set Type 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 ~x &x ?x
  4. x{} x.* x.?
  5. ! * / % ** *% ||
  6. + - ++ +% -%
  7. << >>
  8. &
  9. ^
  10. |
  11. == != < > <= >=
  12. and
  13. or
  14. orelse catch
  15. = *= /= %= += -= <<= >>= &= ^= |=