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

Expressions and operators

Every operator works on known values and on unknowns, with the same meaning. A few operands must be known, as noted below.

Precedence

From loosest to tightest binding:

OperatorsAssociativity
iffleft
impliesright
orleft
xorleft
andleft
notprefix
== != < <= > >= innone: cannot chain
| (bitwise or)left
& (bitwise and)left
<< >>left
+ -left
* / %left
- (negation)prefix
^ (power)right
f(…) a[i] p.x E.Vpostfix

Some consequences:

print 1 + 2 * 3 ^ 2             // 1 + (2 * (3 ^ 2))
print -2 ^ 2, 2 ^ 3 ^ 2         // -(2 ^ 2) and 2 ^ (3 ^ 2)
print 6 & 3 == 2                // (6 & 3) == 2: bitwise binds tighter than ==
print not 1 == 2                // not (1 == 2)
print true or false and false   // true or (false and false)
print false implies false implies false   // false implies (false implies false)
19
-4 512
true
true
true
true

Arithmetic

ExpressionOperandsResultNotes
a + bintintexact
a - bintintexact
a * bintintexact
a / bintintEuclidean division (see below)
a % bintintEuclidean remainder: always in 0..abs(b)
a ^ bintintb must be known and non-negative; at most 64 when a is unknown
-aintint

Integers never overflow: every result is exact, whatever its size.

Euclidean division. a / b and a % b satisfy a == (a / b) * b + a % b with 0 <= a % b < abs(b). For positive operands this is ordinary integer division; for negative ones it differs from C, Java or Rust:

aba / ba % b
7231
-72-41
7-2-31
-7-241

Division by zero. Dividing a known value by zero is an error. Dividing by an unknown adds the requirement that it is not zero. In a branch that may not be taken (if d != 0 { n / d } else { 0 }), the division is guarded like everything else in that branch.

Comparison

ExpressionOperandsResult
a == b, a != bany two values of the same typebool
a < b, a <= b, a > b, a >= bint or charbool

Comparisons do not chain: write a < b and b < c, or b in a + 1..c.

Logic

ExpressionOperandsTrue when
not aboola is false
a and bboolboth are true
a or bboolat least one is true
a xor bboolexactly one is true
a implies bboola is false or b is true
a iff bboolboth have the same value

and, or and implies evaluate b only where it matters: if a decides the result, b is not evaluated at all, and if a is unknown, b is evaluated under the condition that it matters (a for and and implies, not a for or). Requirements that b brings along, such as an index being in range, then only apply under that condition.

Bitwise operators

ExpressionOperandsResultNotes
a & bintintbitwise and
a | bintintbitwise or
a xor bintintbitwise exclusive or
a << kintinta * 2^k; k must be known and non-negative
a >> kintinta / 2^k, rounding down; k must be known and non-negative

Bitwise operators act on the two’s-complement representation, as if integers had infinitely many bits: -5 & 3 == 3, -5 | 3 == -5, -5 xor 3 == -8.

Membership: in

ExpressionTrue when
x in lo..hilo <= x and x < hi (x an int or a char)
x in lo..=hilo <= x and x <= hi
x in arraysome element equals x
c in stringthe character c occurs in the string

Ranges

lo..hi (excluding hi) and lo..=hi (including it) are not values of their own: they appear in types (int[0..10]), loops (for i in 0..n), comprehensions and in.

Indexing and fields

  • xs[i] reads element i (counting from 0) of an array or string. The index may be unknown: that requires it to be in range.
  • p.f reads field f of a struct.
  • E.V is variant V of enum E.

A known index out of range is an error.

if expressions

if condition { value1 } else { value2 }
if c1 { v1 } else if c2 { v2 } else { v3 }

Both branches are required and must have the same type. With an unknown condition, the result is value1 where the condition holds and value2 elsewhere, and each branch is evaluated under its condition.

Comprehensions

[element for x in iterable]
[element for x in iterable if condition for y in iterable if condition …]

Builds an array by looping, with any number of for clauses (nested from left to right), each followed by any number of if filters. An array comprehension’s filters must be known, because the array’s length must be.

The same clauses can be written directly inside the call of an aggregate, or of a function whose only parameter is an array: sum(w[i] for i in 0..n if take[i]). Inside sum, count, all and any, filters may depend on unknowns: a filtered-out element contributes 0 to sum and count, true to all and false to any.

Strings

"text {expression} text" inserts the value of each {expression}, which may be any expression without braces or string literals of its own. Inserted values are shown as print shows them. Write \{ and \} for literal braces.

a + b joins two strings, or two arrays.

Function calls

name(argument, …) calls a built-in, standard library or user function, with positional arguments. Name(…) with a struct’s name builds a struct, with positional or named arguments. See Built-in functions and Standard library.

Holes

? is an expression that stands for a fresh unknown of the type the context expects. See Types.