Operators
Run provides the standard set of operators for arithmetic, comparison, and logic.
Arithmetic operators
Section titled “Arithmetic operators”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.
Comparison operators
Section titled “Comparison operators”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
Section titled “Logical operators”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}Assignment operators
Section titled “Assignment operators”Run supports compound assignment for convenience.
var x int = 10x += 5 // x is now 15x -= 3 // x is now 12x *= 2 // x is now 24x /= 4 // x is now 6x %= 5 // x is now 1Bitwise operators
Section titled “Bitwise operators”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
Section titled “Shift operators”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)}