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 |
---|---|---|---|
| - Integers - Floats | Addition. - Can cause overflow for integers. - Invokes Peer Type Resolution for the operands. - See also @addWithOverflow. |
|
| - Integers | Wrapping Addition. - Guaranteed to have twos-complement wrapping behavior. - Invokes Peer Type Resolution for the operands. - See also @addWithOverflow. |
|
| - Integers - Floats | Subtraction. - Can cause overflow for integers. - Invokes Peer Type Resolution for the operands. - See also @subWithOverflow. |
|
| - Integers | Wrapping Subtraction. - Guaranteed to have twos-complement wrapping behavior. - Invokes Peer Type Resolution for the operands. - See also @subWithOverflow. |
|
| - Integers - Floats | Negation. - Can cause overflow for integers. |
|
| - Integers | Wrapping Negation. - Guaranteed to have twos-complement wrapping behavior. |
|
| - Integers - Floats | Multiplication. - Can cause overflow for integers. - Invokes Peer Type Resolution for the operands. - See also @mulWithOverflow. |
|
| - Integers | Wrapping Multiplication. - Guaranteed to have twos-complement wrapping behavior. - Invokes Peer Type Resolution for the operands. - See also @mulWithOverflow. |
|
| - 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. |
|
| - 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. |
|
| - 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.
|
|
| - Integers | Bit Shift Right.
- b must be comptime-known or have a type with log2 number of bits as a .
- See also @shrExact.
|
|
| - Integers | Bitwise AND. - Invokes Peer Type Resolution for the operands. |
|
| - Integers | Bitwise OR. - Invokes Peer Type Resolution for the operands. |
|
| - Integers | Bitwise XOR. - Invokes Peer Type Resolution for the operands. |
|
| - Integers | Bitwise NOT. |
|
| - 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. |
|
| - Optionals | Equivalent to:
|
|
| - 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 . |
|
| - bool | If a is false , returns false without evaluating b . Otherwise, returns b . |
|
| - bool | If a is true , returns true without evaluating b . Otherwise, returns b . |
|
| - bool | Boolean NOT. |
|
| - Integers - Floats - bool - type | Returns true if a and b are equal, otherwise returns false . Invokes Peer Type Resolution for the operands. |
|
| - Optionals | Returns true if a is null , otherwise returns false . |
|
| - Integers - Floats - bool - type | Returns false if a and b are equal, otherwise returns true . Invokes Peer Type Resolution for the operands. |
|
| - Integers - Floats | Returns true if a is greater than b, otherwise returns false . Invokes Peer Type Resolution for the operands. |
|
| - Integers - Floats | Returns true if a is greater than or equal to b, otherwise returns false . Invokes Peer Type Resolution for the operands. |
|
| - Integers - Floats | Returns true if a is less than b, otherwise returns false . Invokes Peer Type Resolution for the operands. |
|
| - Integers - Floats | Returns true if a is less than or equal to b, otherwise returns false . Invokes Peer Type Resolution for the operands. |
|
| - Arrays | Array concatenation.
- Only available when a and b are compile-time known.
|
|
| - Arrays | Array multiplication.
- Only available when a and b are compile-time known.
|
|
| - Pointers | Pointer dereference. |
|
| All types | Address of. |
|
| - Error Set Type | Merging Error Sets |
|
Precedence
x() x[] x.y
a!b
!x -x -%x ~x &x ?x
x{} x.* x.?
! * / % ** *% ||
+ - ++ +% -%
<< >>
& ^ |
== != < > <= >=
and
or
orelse catch
= *= /= %= += -= <<= >>= &= ^= |=