Skip to content

Operators

Precedence decides how an expression groups when it mixes operators. From lowest to highest:

PrecedenceOperatorsDescription
1|>Pipeline
2||Logical or
3&&Logical and
4== != < > <= >=Comparison
5.. ..=Range
6+ - | ^Add/subtract, bitwise or/xor
7* / % << >> & &^Multiply/divide, shifts, bitwise and/and-not
8asType conversion
9- ! ^ &Prefix (negation, not, bitwise not, reference)
10. () [] ? .*Postfix (access, call, index, propagate, deref)

All binary operators are left-associative, so a repeated operator groups from the left.

a + b * cparses as a + (b * c)
a && b || cparses as (a && b) || c
a + b |> fparses as f(a + b)
0..1 + 2parses as 0..(1 + 2)

+, -, *, /, % require both operands to be the same numeric type. Unary - negates a number. An untyped numeric literal adapts to the other operand.

let size: int64 = 4
let ratio: float64 = 1.5
let next = size + 1
int64
let double = ratio * 2
float64

+ also concatenates strings:

"hello" + ", " + "world""hello, world"

== and != compare two values of the same comparable type. <, >, <=, >= compare numeric types and strings. All of them return bool.

2 == 2true
"apple" < "banana"true, compared byte by byte

&& is true when both operands are true, || is true when at least one is, and ! negates. All require bool operands. && and || short-circuit, i.e. if the left operand already determines the result, the right operand is not evaluated.

let items = [3, 0, 7]
let i = 5
if i < items.length() && items[i] > 0 {
false, so items[i] > 0 is skipped
fmt.Println(items[i])
}

& (and), | (or), ^ (xor), and &^ (and-not) combine two integers bit by bit. Both operands must have the same integer type. Unary ^ flips every bit. << and >> shift the left operand by the right operand, which can be any integer type. The result has the left operand’s type.

let value = 0b1010
value & 0b01100b0010
value | 0b01010b1111
value ^ 0b01100b1100
value &^ 0b00100b1000
value << 20b101000
value >> 10b0101
^value-11

Slices and arrays support integer indexing:

let scores = [90, 85, 77]
let rgb: Array<int, 3> = [255, 128, 0]
scores[0]90
rgb[1]128

Maps support key indexing:

let mut ages = Map.new<string, int>()
ages["Alice"] = 20
ages["Alice"]20

Indexing a slice or array panics if the index is out of bounds. Indexing a map returns the zero value if the key is missing. For access that cannot panic, use .get(), which returns an Option<T>.

let scores = [90, 85, 77]
let ages = Map.new<string, int>()
scores[5]panic: index out of range
ages["Bob"]0
scores.get(5)None

A range is a span of values from a start to an end. One bound can be left out.

SyntaxTypeDescription
start..endRange<T>Exclusive upper bound
start..=endRangeInclusive<T>Inclusive upper bound
start..RangeFrom<T>No upper bound
..endRangeTo<T>Exclusive, no lower bound
..=endRangeToInclusive<T>Inclusive, no lower bound

A range can drive a for loop or index a slice:

let slice = items[1..4]elements at indices 1, 2, 3

Slice sub-slicing is safe by default. The resulting sub-slice has its capacity capped to its length, so append on a sub-slice always allocates a new backing array and never silently mutates the original.

& creates a Ref<T> that points at a value instead of copying it. .* reads the value back.

let count = 42
let ref = &countRef<int>
let value = ref.*42

|> passes the value on its left as the first argument of the function on its right.

x |> fsame as f(x)
x |> f(y)same as f(x, y)
x |> f(y, z)same as f(x, y, z)

To pass the value on the left in another position, mark that position with _:

x |> f(y, _)same as f(y, x)
x |> f(y, _, z)same as f(y, x, z)

Chains read top to bottom:

let result = items
|> filter(is_valid)
|> map(transform)
|> sum()
same as sum(map(filter(items, is_valid), transform))

Lambdas are not allowed as pipeline targets.

The ? operator propagates a failure.

The as operator converts between numeric types, and between strings and byte or rune slices.

= assigns to a mutable target.

let mut name = "Alice"
name = "Bob"

+=, -=, *=, /=, %=, &=, |=, ^=, &^=, <<=, >>= combine an operation with an assignment.

let mut count = 0
let mut total = 10
count += 1count is 1
total *= 2total is 20