Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Variables & Bindings

Let Bindings

Variables are introduced with let. They are immutable by default.

let x = 5
let name = "Alice"

Optional type annotation:

let x: i64 = 5

Bindings can use any expression:

let x = 5 + 3
let y = x * 2
let result = compute(42)
let s = "hello".to_upper()

Var Bindings

Use var for mutable state. A var binding creates a mutable ref cell:

var count = 0
count = count + 1
count += 1

Optional type annotation:

var total: i64 = 0

Destructuring

Positional products:

let .{ a, b } = get_pair()
let .{ a, .{ b, c } } = get_nested()
let .{ _, b } = get_pair()          // wildcard discards first element

Named products and data values:

let Point.{ .x = x, .y = y } = get_point()
let Point.{ .x = px, .y = py } = get_point()   // rename fields
let Config.{ .name = name } = get_config()

Constants

Top-level compile-time values with required type annotations:

const MAX_SIZE: i64 = 1024
const PI: f64 = 3.14159
const GREETING: String = "hello"
const YES: bool = true
const BYTE_A: u8 = 'a'

Constants support all literal forms:

const HEX: i64 = 0xFF
const BIN: i64 = 0b1010
const OCT: i64 = 0o77
const SCI: f64 = 1.5e10
const TRIPLE: String = """triple quoted"""

Visibility and attributes:

pvt const SECRET: i64 = 42

#[deprecated]
const OLD_VALUE: i64 = 0