Operators
Precedence
Section titled “Precedence”Precedence decides how an expression groups when it mixes operators. From lowest to highest:
| Precedence | Operators | Description |
|---|---|---|
| 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 |
| 8 | as | Type 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) || ca + b |> fparses as f(a + b)0..1 + 2parses as 0..(1 + 2)Value operators
Section titled “Value operators”Arithmetic
Section titled “Arithmetic”+, -, *, /, % 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 = 4let ratio: float64 = 1.5let next = size + 1int64let double = ratio * 2float64+ also concatenates strings:
"hello" + ", " + "world""hello, world"Comparison
Section titled “Comparison”== 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 byteLogical
Section titled “Logical”&& 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 = 5if i < items.length() && items[i] > 0 {false, so items[i] > 0 is skipped fmt.Println(items[i])}Bitwise
Section titled “Bitwise”& (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 = 0b1010value & 0b01100b0010value | 0b01010b1111value ^ 0b01100b1100value &^ 0b00100b1000value << 20b101000value >> 10b0101^value-11Access operators
Section titled “Access operators”Indexing
Section titled “Indexing”Slices and arrays support integer indexing:
let scores = [90, 85, 77]let rgb: Array<int, 3> = [255, 128, 0]
scores[0]90rgb[1]128Maps support key indexing:
let mut ages = Map.new<string, int>()ages["Alice"] = 20
ages["Alice"]20Indexing 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 rangeages["Bob"]0
scores.get(5)NoneA range is a span of values from a start to an end. One bound can be left out.
| Syntax | Type | Description |
|---|---|---|
start..end | Range<T> | Exclusive upper bound |
start..=end | RangeInclusive<T> | Inclusive upper bound |
start.. | RangeFrom<T> | No upper bound |
..end | RangeTo<T> | Exclusive, no lower bound |
..=end | RangeToInclusive<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, 3Slice 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.
Ref and deref
Section titled “Ref and deref”& creates a Ref<T> that points at a value instead of copying it. .* reads the value back.
let count = 42let ref = &countRef<int>
let value = ref.*42Expression operators
Section titled “Expression operators”Pipeline
Section titled “Pipeline”|> 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.
Error propagation
Section titled “Error propagation”The ? operator propagates a failure.
Type conversion
Section titled “Type conversion”The as operator converts between numeric types, and between strings and byte or rune slices.
Assignment operators
Section titled “Assignment operators”= assigns to a mutable target.
let mut name = "Alice"name = "Bob"+=, -=, *=, /=, %=, &=, |=, ^=, &^=, <<=, >>= combine an operation with an assignment.
let mut count = 0let mut total = 10
count += 1count is 1total *= 2total is 20