| Operator | Meaning | Example |
+ | addition | x + y |
- | subtraction | x - y |
* | multiplication | x * y |
/ | division | x / y |
% | modulo | x % y |
** | exponentiation | x ** y |
| Operator | Meaning | Example |
== | equal | x == y |
!= | not equal | x != y |
< | less than | x < y |
<= | less than or equal | x <= y |
> | greater than | x > y |
>= | greater than or equal | x >= y |
| Operator | Meaning | Example |
and | logical AND (short-circuit) | x and y |
or | logical OR (short-circuit) | x or y |
not | logical NOT | not x |
and and or are keywords, not symbols. They short-circuit: the right operand is only evaluated if needed.
| Operator | Meaning | Example |
& | bitwise AND | x & y |
| | bitwise OR | x | y |
^ | bitwise XOR | x ^ y |
~ | bitwise NOT | ~x |
<< | left shift | x << y |
>> | right shift | x >> y |
| Operator | Meaning | Example |
<> | append | xs <> ys |
| Operator | Meaning | Example |
- | negation | -x |
not | logical NOT | not x |
~ | bitwise NOT | ~x |
& | address-of (linear) | &x |
* | dereference (linear) | *p |
From highest to lowest binding power:
| Precedence | Operators | Associativity |
| 25 | . () ? | left (postfix) |
| 23 | - not ~ & * (unary) | prefix |
| 21 | ** | right |
| 19 | * / % | left |
| 17 | + - <> | left |
| 15 | << >> | left |
| 13 | & | left |
| 11 | ^ | left |
| 9 | | | left |
| 7 | == != < <= > >= | left |
| 5 | and | left |
| 3 | or | left |
| 1 | = += -= *= /= %= | right |
Parentheses override precedence:
x + y * z // y * z first
(x + y) * z // x + y first
-a + b ** 2 // (-a) + (b ** 2)
Available to var only
| Operator | Desugars to |
x += y | x = x + y |
x -= y | x = x - y |
x *= y | x = x * y |
x /= y | x = x / y |
x %= y | x = x % y |
The postfix ? operator propagates errors from error union values:
fn read_config(path: String) -> !Config
let contents = read_file(path)? // returns error early if read fails
let config = parse(contents)?
config