Variables
Run has two keywords for declaring variables: var for mutable bindings and let for immutable bindings.
Mutable variables with var
Section titled “Mutable variables with var”Use var to declare a variable that can be reassigned. Variables declared without an initializer are zero-initialized.
var x int // x is 0var y f64 // y is 0.0var z bool // z is falsevar s string // s is ""You can also provide an initial value:
var x int = 42var name string = "Run"Immutable variables with let
Section titled “Immutable variables with let”Use let to declare a variable that cannot be reassigned. An initializer is required — the value is fixed once set.
let pi f64 = 3.14159let name string = "Run"let x = 42 // type inferredAttempting to reassign a let variable is a compile-time error. The compiler shows where the variable was originally defined and suggests using var instead.
Short declarations
Section titled “Short declarations”The := operator declares a mutable variable and infers its type from the right-hand side.
package main
use "fmt"
pub fun main() { x := 42 name := "Run" pi := 3.14159
fmt.println(x) fmt.println(name) fmt.println(pi)}Short declarations are the most common way to declare variables in Run. They are equivalent to var with type inference.
Zero values
Section titled “Zero values”Variables declared with var without an explicit initial value are given their zero value:
0for numeric typesfalseforbool""forstringnullfor nullable types