Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Operators

Arithmetic

OperatorMeaningExample
+additionx + y
-subtractionx - y
*multiplicationx * y
/divisionx / y
%modulox % y
**exponentiationx ** y

Comparison

OperatorMeaningExample
==equalx == y
!=not equalx != y
<less thanx < y
<=less than or equalx <= y
>greater thanx > y
>=greater than or equalx >= y

Logical

OperatorMeaningExample
andlogical AND (short-circuit)x and y
orlogical OR (short-circuit)x or y
notlogical NOTnot x

and and or are keywords, not symbols. They short-circuit: the right operand is only evaluated if needed.

Bitwise

OperatorMeaningExample
&bitwise ANDx & y
|bitwise ORx | y
^bitwise XORx ^ y
~bitwise NOT~x
<<left shiftx << y
>>right shiftx >> y

Other

OperatorMeaningExample
<>appendxs <> ys

Unary

OperatorMeaningExample
-negation-x
notlogical NOTnot x
~bitwise NOT~x
&address-of (linear)&x
*dereference (linear)*p

Precedence

From highest to lowest binding power:

PrecedenceOperatorsAssociativity
25. () ?left (postfix)
23- not ~ & * (unary)prefix
21**right
19* / %left
17+ - <>left
15<< >>left
13&left
11^left
9|left
7== != < <= > >=left
5andleft
3orleft
1= += -= *= /= %=right

Parentheses override precedence:

x + y * z       // y * z first
(x + y) * z     // x + y first
-a + b ** 2     // (-a) + (b ** 2)

Compound Assignment

Available to var only

OperatorDesugars to
x += yx = x + y
x -= yx = x - y
x *= yx = x * y
x /= yx = x / y
x %= yx = x % y

Try Operator

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