Operators and precedence
This page lists every operator and how tightly it binds.
Binary operator precedence
Section titled “Binary operator precedence”Precedence climbs from loosest (1) to tightest (10). Higher numbers bind
tighter, so a + b * c parses as a + (b * c).
| Level | Operators | Meaning |
|---|---|---|
| 1 | .. ..= || | ranges (exclusive / inclusive), logical OR |
| 2 | && | logical AND |
| 3 | == != | equality |
| 4 | < <= > >= | comparison |
| 5 | | | bitwise OR |
| 6 | ^ | bitwise XOR |
| 7 | & | bitwise AND |
| 8 | << >> | shifts |
| 9 | + - | add, subtract |
| 10 | * / % | multiply, divide, remainder |
Ranges (0..10, 0..=10) sit at the lowest level, alongside ||. Use them in
for i: int in 0..10 and in match arms (400..=499 => ...).
Unary and postfix
Section titled “Unary and postfix”- Unary
-(negate) and!(logical not). - Postfix
.field/.method(...),[index], and call(...). ?propagates errors on aResult/Option. Onerr/noneit returns up; otherwise it unwraps. See Option and Result.asis an explicit numeric cast or upcast:qty as decimal.as?is a checked downcast returning anOption. See Interfaces and inheritance.
Postfix, as, as?, and ? bind tighter than the binary operators above.
Assignment
Section titled “Assignment”Assignment is a statement, not part of the precedence climb:
= += -= *= /= %=var n: int = 1n += 4n *= 2 // n is now 10Bracket assignment works on lists, maps, and fixed arrays: xs[i] = v,
m[key] = v. List and Map bracket assignment has no compound form (no
xs[i] += 1); fixed arrays do support compound element assignment, because
their element is a real inline place.
Number and comparison rules
Section titled “Number and comparison rules”- Fixed-width integer
+,-,*, unary-, and the bit operations wrap to the type’s width. Shift counts are masked bywidth - 1. Divide or modulo by zero panics. - There are no implicit numeric conversions. Mixing
int,float, anddecimalneeds an explicitas. - Float comparisons follow IEEE-754: a NaN operand makes
==,<,<=,>,>=false and!=true.
See Numbers and decimal.
No operators for these
Section titled “No operators for these”- No
+on strings. Build strings with interpolation ("a {b} c"),std.fmt, orlist.join(sep). - No
++/--. Use+= 1/-= 1. - No ternary. Use
if/matchin value position (see Control flow).