Operators
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
| Table of OperatorsSyntax | Relevant Types | Description | Example |
|---|---|---|---|
a + b
a += b
``` |
* Integers
* Floats
| Addition.
* Can cause overflow for integers.
* Invokes Peer Type Resolution for the operands.
* See also @addWithOverflow.
| ```
2 + 5 == 7
a +% b
a +%= b
``` |
* Integers
| Wrapping Addition.
* Guaranteed to have twos-complement wrapping behavior.
* Invokes Peer Type Resolution for the operands.
* See also @addWithOverflow.
| ```
@as(u32, std.math.maxInt(u32)) +% 1 == 0
a +| b
a +|= b
``` |
* Integers
| Saturating Addition.
* Invokes Peer Type Resolution for the operands.
| ```
@as(u32, std.math.maxInt(u32)) +| 1 == @as(u32, std.math.maxInt(u32))
a - b
a -= b
``` |
* Integers
* Floats
| Subtraction.
* Can cause overflow for integers.
* Invokes Peer Type Resolution for the operands.
* See also @subWithOverflow.
| ```
2 - 5 == -3
a -% b
a -%= b
``` |
* Integers
| Wrapping Subtraction.
* Guaranteed to have twos-complement wrapping behavior.
* Invokes Peer Type Resolution for the operands.
* See also @subWithOverflow.
| ```
@as(u32, 0) -% 1 == std.math.maxInt(u32)
a -| b
a -|= b
``` |
* Integers
| Saturating Subtraction.
* Invokes Peer Type Resolution for the operands.
| ```
@as(u32, 0) -| 1 == 0
-a
``` |
* Integers
* Floats
| Negation.
* Can cause overflow for integers.
| ```
-1 == 0 - 1
-%a
``` |
* Integers
| Wrapping Negation.
* Guaranteed to have twos-complement wrapping behavior.
| ```
-%@as(i32, std.math.minInt(i32)) == std.math.minInt(i32)
a b
a = b
``` |
* Integers
* Floats
| Multiplication.
* Can cause overflow for integers.
* Invokes Peer Type Resolution for the operands.
* See also @mulWithOverflow.
| ```
2 5 == 10
a % b
a %= b
``` |
* Integers
| Wrapping Multiplication.
* Guaranteed to have twos-complement wrapping behavior.
* Invokes Peer Type Resolution for the operands.
* See also @mulWithOverflow.
| ```
@as(u8, 200) % 2 == 144
a | b
a |= b
``` |
* Integers
| Saturating Multiplication.
* Invokes Peer Type Resolution for the operands.
| ```
@as(u8, 200) | 2 == 255
a / b
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.
* Signed integer operands must be comptime-known and positive. In other cases, use @divTrunc, @divFloor, or @divExact instead.
* Invokes Peer Type Resolution for the operands.
| ```
10 / 5 == 2
a % b
a %= b
``` |
* Integers
* Floats
| Remainder Division.
* Can cause Division by Zero for integers.
* Can cause Division by Zero for floats in FloatMode.Optimized Mode.
* Signed or floating-point operands must be comptime-known and positive. In other cases, use @rem or @mod instead.
* Invokes Peer Type Resolution for the operands.
| ```
10 % 3 == 1
a << b
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 << 8 == 256
a <<| b
a <<|= b
``` |
* Integers
| Saturating Bit Shift Left.
* See also @shlExact.
* See also @shlWithOverflow.
| ```
@as(u8, 1) <<| 8 == 255
a >> b
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.
| ```
10 >> 1 == 5
a & b
a &= b
``` |
* Integers
| Bitwise AND.
* Invokes Peer Type Resolution for the operands.
| ```
0b011 & 0b101 == 0b001
a | b
a |= b
``` |
* Integers
| Bitwise OR.
* Invokes Peer Type Resolution for the operands.
| ```
0b010 | 0b100 == 0b110
a ^ b
a ^= b
``` |
* Integers
| Bitwise XOR.
* Invokes Peer Type Resolution for the operands.
| ```
0b011 ^ 0b101 == 0b110
~a
``` |
* Integers
| Bitwise NOT.| ```
~@as(u8, 0b10101111) == 0b01010000
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.| ```
const value: ?u32 = null;
const unwrapped = value orelse 1234;
unwrapped == 1234
a.?
``` |
* Optionals
| Equivalent to: ```
a orelse unreachable
``` | ```
const value: ?u32 = 5678;
value.? == 5678
a catch b
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`.| ```
const value: anyerror!u32 = error.Broken;
const unwrapped = value catch 1234;
unwrapped == 1234
a and b
``` |
* bool
| If `a` is `false`, returns `false` without evaluating `b`. Otherwise, returns `b`.| ```
(false and true) == false
a or b
``` |
* bool
| If `a` is `true`, returns `true` without evaluating `b`. Otherwise, returns `b`.| ```
false or true == true
!a
``` |
* bool
| Boolean NOT.| ```
!false == true
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) == true
a == null
``` |
* Optionals
| Returns `true` if a is `null`, otherwise returns `false`.| ```
const value: ?u32 = null;
value == null
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) == false
a > b
``` |
* Integers
* Floats
| Returns `true` if a is greater than b, otherwise returns `false`. Invokes Peer Type Resolution for the operands.| ```
(2 > 1) == true
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.| ```
(2 >= 1) == true
a < b
``` |
* Integers
* Floats
| Returns `true` if a is less than b, otherwise returns `false`. Invokes Peer Type Resolution for the operands.| ```
(1 < 2) == true>
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 <= 2) == true
a ++ b
``` |
* Arrays
| Array concatenation.
* Only available when `a` and `b` are compile-time known.
| ```
const mem = @import(“std”).mem;
const array1 = []u32{1,2};
const array2 = []u32{3,4};
const together = array1 ++ array2;
mem.eql(u32, &together, &[_]u32{1,2,3,4})
a b
``` |
* Arrays
| Array multiplication.
* Only available when `a` and `b` are compile-time known.
| ```
const mem = @import(“std”).mem;
const pattern = “ab” 3;
mem.eql(u8, pattern, “ababab”)
a.
``` |
* Pointers
| Pointer dereference.| ```
const x: u32 = 1234;
const ptr = &x;
ptr. == 1234
&a
``` | All types| Address of.| ```
const x: u32 = 1234;
const ptr = &x;
ptr. == 1234
a || b
``` |
* Error Set Type
| Merging Error Sets| ```
const A = error{One};
const B = error{Two};
(A || B) == error{One, Two}
Precedence
x() x[] x.y x.* x.?
a!b
x{}
!x -x -%x ~x &x ?x
* / % ** *% *| ||
+ - ++ +% -% +| -|
<< >> <<|
& ^ | orelse catch
== != < > <= >=
and
or
= *= *%= *|= /= %= += +%= +|= -= -%= -|= <<= <<|= >>= &= ^= |=
