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
| Name | Syntax | Types | Remarks | Example |
|---|---|---|---|---|
| Addition | |
|
|
|
| Wrapping Addition | |
|
|
|
| Saturating Addition | |
|
|
|
| Subtraction | |
|
|
|
| Wrapping Subtraction | |
|
|
|
| Saturating Subtraction | |
|
|
|
| Negation | |
|
|
|
| Wrapping Negation | |
|
|
|
| Multiplication | |
|
|
|
| Wrapping Multiplication | |
|
|
|
| Saturating Multiplication | |
|
|
|
| Division | |
|
|
|
| Remainder Division | |
|
|
|
| Bit Shift Left | |
|
|
|
| Saturating Bit Shift Left | |
|
|
|
| Bit Shift Right | |
|
|
|
| Bitwise And | |
|
|
|
| Bitwise Or | |
|
|
|
| Bitwise Xor | |
|
|
|
| Bitwise Not | |
|
||
| Defaulting Optional Unwrap | |
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.
|
|
|
| Optional Unwrap | |
Equivalent to:
|
|
|
| Defaulting Error Unwrap | |
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.
|
|
|
| Logical And | |
If a is false, returns false
without evaluating b. Otherwise, returns b.
|
|
|
| Logical Or | |
If a is true,
returns true without evaluating
b. Otherwise, returns
b.
|
|
|
| Boolean Not | |
|
||
| Equality | |
Returns true if a and b are equal, otherwise returns false.
Invokes Peer Type Resolution for the operands.
|
|
|
| Null Check | |
Returns true if a is null, otherwise returns false.
|
|
|
| Inequality | |
Returns false if a and b are equal, otherwise returns true.
Invokes Peer Type Resolution for the operands.
|
|
|
| Non-Null Check | |
Returns false if a is null, otherwise returns true.
|
|
|
| Greater Than | |
Returns true if a is greater than b, otherwise returns false.
Invokes Peer Type Resolution for the operands.
|
|
|
| Greater or Equal | |
Returns true if a is greater than or equal to b, otherwise returns false.
Invokes Peer Type Resolution for the operands.
|
|
|
| Less Than | |
Returns true if a is less than b, otherwise returns false.
Invokes Peer Type Resolution for the operands.
|
|
|
| Lesser or Equal | |
Returns true if a is less than or equal to b, otherwise returns false.
Invokes Peer Type Resolution for the operands.
|
|
|
| Array Concatenation | |
|
|
|
| Pointer Dereference | |
Pointer dereference. |
|
|
| Address Of | |
All types |
|
|
| Error Set Merge | |
Merging Error Sets |
|
Precedence
x() x[] x.y x.* x.?
a!b
x{}
!x -x -%x ~x &x ?x
* / % *% *| ||
+ - ++ +% -% +| -|
<< >> <<|
& ^ | orelse catch
== != < > <= >=
and
or
= *= *%= *|= /= %= += +%= +|= -= -%= -|= <<= <<|= >>= &= ^= |=