Skip to content

Operators

Run provides the standard set of operators for arithmetic, comparison, and logic.

package main
use "fmt"
pub fun main() {
a := 10
b := 3
fmt.println(a + b) // 13 addition
fmt.println(a - b) // 7 subtraction
fmt.println(a * b) // 30 multiplication
fmt.println(a / b) // 3 integer division
fmt.println(a % b) // 1 remainder
}

Integer division truncates toward zero. Use floating-point types when you need fractional results.

Comparisons return a bool value.

package main
use "fmt"
pub fun main() {
x := 5
y := 10
fmt.println(x == y) // false equal
fmt.println(x != y) // true not equal
fmt.println(x < y) // true less than
fmt.println(x > y) // false greater than
fmt.println(x <= y) // true less or equal
fmt.println(x >= y) // false greater or equal
}

Equality operators (==, !=) work on any compatible pair of types. Ordering operators (<, >, <=, >=) require numeric operands — comparing strings or other non-numeric types with ordering operators is a compile-time error.

Logical operators work on bool values and short-circuit.

package main
use "fmt"
pub fun main() {
a := true
b := false
fmt.println(a and b) // false
fmt.println(a or b) // true
fmt.println(not a) // false
}

Run supports compound assignment for convenience.

var x int = 10
x += 5 // x is now 15
x -= 3 // x is now 12
x *= 2 // x is now 24
x /= 4 // x is now 6
x %= 5 // x is now 1

Bitwise operators work on integers and are often used with bit masks.

package main
use "fmt"
pub fun main() {
a := 60 // 0011 1100
b := 13 // 0000 1101
fmt.println(a & b) // 12 0000 1100 bitwise AND
fmt.println(a | b) // 61 0011 1101 bitwise OR
fmt.println(a ^ b) // 49 0011 0001 bitwise XOR
fmt.println(~a) // bitwise NOT
}

Shift operators move bits left or right.

package main
use "fmt"
pub fun main() {
// Shift operators
let a = 1 << 4 // 16
let b = 16 >> 2 // 4
fmt.println(a)
fmt.println(b)
}