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

Expressions and statements

Statements

Statements are separated by newlines. A semicolon is rejected, and two statements on one line are a compile error. An expression may continue onto the next line inside (, [, <, {, or after a binary operator.

Statements: let, destructuring let, assignment to a let mut, return, if, for, while, break, continue, and expressions.

let point = Point(x: 1, y: 2)
let Point { x, y as py } = point
let mut total = 0
total = total + x

Only the body of a function, an if branch, and a match branch evaluates to their last expression. A standalone block is statement-only; let x = { … } is illegal.

match

match must be exhaustive at the top variant level. Patterns are _, None, a variant, or a variant with nested patterns (Some(Some(x)), Ok(Value.Int(n))). Struct patterns are allowed for struct scrutinees: Point { x, y } binds fields by name, and Point { x: px, y } renames.

match role {
  Role.Admin     => "admin"
  Role.Member(n) => `member-${n}`
  Role.Guest     => "guest"
}
  • Alternatives may share an arm with | when they bind the same names and types.
  • Guards use pat if cond => …, are evaluated after binding, and do not close exhaustiveness.
  • A struct pattern with only bindings and wildcards is irrefutable.
  • Nested exhaustiveness is not checked. An arm that does not match falls through; if nothing matches, the runtime reports an unreachable trap.

Loops

for book in books { … }
for key in map { … }       // keys
for key in dict { … }      // keys
for item in set { … }      // sorted order
while ready { … }

break and continue apply to the nearest loop. Loop state changes only through local let mut.

Templates

A plain template interpolates any value with Display:

`Hi ${user.name}, total ${total}`

Multiline templates are verbatim: newlines and indentation are preserved. Use .trim_indent() to remove indentation.

A tagged template calls a package-level function with signature (List<Str>, List<T>) -> R:

let query = sql`SELECT * FROM users WHERE id = ${Arg.I(id)}`

Every ${…} must be exactly T. There is no implicit Display, no automatic Some, and no automatic variant selection. Mixed types use an enum as T. The invariant parts.len() == values.len() + 1 always holds.

JSX

JSX is an ordinary struct constructor:

<Element tag="div" class_name="user">
  <Element tag="span">{user.name}</Element>
</Element>

The tag name is a struct in scope and must be PascalCase. Attributes are named arguments. Children are collected into a children argument only when the struct declares that field. There is no lifecycle, render function, or framework.

Complete example

package nested_match_case {
  export fn unwrap_nested(x: Option<Option<Int>>) -> Int {
    match x {
      Some(Some(n)) => n
      Some(None) => 0
      None => -1
    }
  }

  export fn kind(text: Str) -> Str {
    match json.parse(text) {
      Ok(Value.Int(n)) => `int ${n}`
      Ok(Value.Str(s)) => `str ${s}`
      Ok(other) => "other"
      Err(message) => message
    }
  }

  test "patterns nest to any depth" {
    expect.eq(unwrap_nested(Some(Some(3))), 3)
    expect.eq(unwrap_nested(Some(None)), 0)
    expect.eq(unwrap_nested(None), -1)
    expect.eq(kind("1"), "int 1")
    expect.eq(kind(`"x"`), "str x")
    expect.eq(kind("true"), "other")
  }
}