Contents

This chapter is a reference, not a tutorial. It collects the surface syntax and the operator table in one place. The explanatory chapters remain the place to learn the semantics.

Lexical Elements#

Element Form
Line comment // text to the end of the line
Block comment /* text */, non-nesting
Identifier ASCII letter or _, then ASCII letters, digits, or _
Integer Decimal or 0x hexadecimal, _ separators allowed
Float Decimal with . or exponent, _ separators allowed
Boolean true, false
String "text" with the escapes \n \t \r \0 \\ \" \xNN
Multiline string \\first line followed by lines beginning with optional indentation and \\
Interpolated string $"text {expression} text"
Rune No literal; use rune conversion functions
Byte buffer No literal; use bytes::new or bytes::from_str
C string No literal; use cstr::from_str or cstr::null
Pointer No literal; use ptr::null

Whitespace and newlines separate tokens. There are no semicolons. A control body may be a brace block or a single statement indented on a following line.

Keywords and Contextual Words#

Context Words
Top level fn, type, const, contract, trait, impl, use, test, extern, pub, pure
Contracts pre, post, invariant
Statements const, var, if, else, for, in, break, continue, return, defer, assert
Expressions if, else, match, fn, is, true, false, None, Some, Success, Error, cast, trunc, truncate
Types enum, extern, of
Conditional compilation @cfg, all, any, not

interface and import are rejected. while is not a keyword; use for condition.

Type Syntax#

Form Meaning
i8, i16, i32, i64 Signed integers
u8, u16, u32, u64 Unsigned integers
f64 Floating point
bool, str, rune, cstr, ptr, bytes, void Built-in types
List[T] List of T
Option[T] Optional T
Result[T, E] Success T or error E
fn(T, U) R Function type
Name Declared record, sum, enum, or refinement
Name[A, B] Generic instantiation
T Type parameter

Declarations#

Declaration Form
Function fn name[T Bound](param Type, ...) Ret : contract ... => expr
Block function fn name(...) Ret { ... }
Pure function pure fn name(...) ...
Public function pub fn name(...) ...
Extern function extern (C) name(param Type, ...) Ret = "symbol"
Module constant [pub] const Name Type = expr
Record type Name { field Type ... }
Record with invariant type Name : invariant expr { field Type ... }
Generic record type Name[T] { ... }
Sum type type Name = | Variant | Variant(field Type, ...)
Generic sum type Name[T] = | ...
Enum type Name enum { Variant Variant }
Extern record type Name extern { field Type ... }
Refinement type Name = Base : invariant expr
Contract contract Name(params) [: Base(args), ...] { pre ... post ... }
Trait trait Name[T] [: Base, ...] { fn method(...) ... }
Implementation impl Target[Args] of Trait[Args] { fn method(...) ... }
Import use path or use path {Name, Name}
Test test "name" { ... }
Conditional item @cfg(condition) declaration

Statements#

Statement Form
Immutable local const name [Type] = expr
Mutable local var name [Type] = expr
Assignment lvalue = expr
Compound assignment lvalue += expr, -=, *=, /=, %=
Increment / decrement name++, name--
Return return or return expr
Assertion assert expr, test blocks only
Range loop for name in lo..hi { ... }
Collection loop for name in list { ... }
Condition loop for condition { ... }
Infinite loop for { ... }
Loop invariant for ... : invariant expr { ... }
Break / continue break, continue
Defer call defer call(...)
Defer block defer { ... }
Conditional block @cfg(condition) { ... }
Block { ... }
Expression statement expr

Expressions#

Expression Form
Literals Integer, float, boolean, string, interpolated string
Names Variable, function, constructor
Grouping (expr)
Call callee(args)
Method call receiver.method(args)
Field access value.field
Index value[index]
List literal [a, b, c]
Record literal Type { field = value, ... }
Generic record literal Type[A, B] { field = value, ... }
Refinement literal Type { value }
Checked construction Type { ... }?
Cast cast[Type](expr)
Truncation trunc[Type](expr) or truncate[Type](expr)
Pattern test expr is Pattern
Result propagation expr?
Conditional if cond { ... } else { ... }
Match match value { Pattern => expr ... }
Block expression { ... }
Lambda fn(params) => expr or fn(params) { ... }
Interpolation $"text {expr} text"
Configuration expression @cfg { condition => expr ... }

Operator Precedence#

From lowest to highest. Binary operators are left-associative; is is a single non-associative test; postfix forms chain left to right.

Level Operators Associativity
1 \|\| Left
2 && Left
3 \| Left
4 ^ Left
5 & Left
6 ==, != Left
7 is Non-associative
8 <, <=, >, >= Left
9 <<, >> Left
10 +, - Left
11 *, /, % Left
12 unary -, !, ~ Right
13 call, field, index, ? Left-to-right chain

Assignment and ++/-- are statements, not operators in the expression table. .. appears only in a range loop header. :: is a qualified-name separator, not an operator.

Operator Semantics#

Operator Operands Result
+ Numeric, str + str, List[T] + List[T] Sum, concatenation, or concatenation
-, *, / Numeric Numeric
% Integer Integer remainder
&, \|, ^, ~ Integer Integer bitwise
<<, >> Integer Shift
<, <=, >, >= Numeric or rune bool
==, != Numeric, bool, str, ptr, cstr, same-type enum, rune bool
&&, \|\| bool bool, short-circuit
! bool bool
unary - Signed integer or f64 Same or promoted type
is Sum, enum, Option, Result, or refinement bool
? Result or checked construction Payload or propagated error

Records, lists, bytes, sums, Option, Result, and functions do not support ==. Strings compare by content; pointers and C strings compare by address; enums compare by variant within the same enum type.

Literal and Conversion Rules#

Source Target Form
Narrow signed integer Wider signed integer Implicit
Narrow unsigned integer Wider unsigned integer Implicit
Wider integer Narrower integer trunc[T](x)
Signed integer Unsigned integer of the same width cast[T](x)
Integer f64 cast[f64](x)
f64 Integer cast[T](x)
Constant integer Any integer type that can represent it Implicit at the declaration

A narrowing cast is rejected. A trunc that does not narrow is rejected. Casting to or from str is rejected. Boolean, record, list, sum, and enum values cannot be cast to numeric types.

Compile-Time Configuration#

Form Position
@cfg(condition) Before a top-level declaration
@cfg(condition) { ... } Statement block
@cfg { condition => expr ... } Expression

The condition language supports os, arch, family, endian, ptr_width, boolean and integer literals, !, -, &&, ||, ==, !=, <, <=, >, >=, and the predicates all, any, and not. The condition must produce a boolean.

Grammar Notes#

A record literal is recognized when an identifier is followed by { and the identifier is a known record, is uppercase-initial, has explicit type arguments, or was imported. A refinement literal is recognized only when the type is already known as a refinement at parse time, so declare a refinement before using its value-style constructor.

A function contract clause is : pre expr, : post expr, : post name => expr, or : Name(args). A braced clause after pre or post is a sequence of separate conjunctive clauses, both on a function and inside a contract declaration.

A match arm is Pattern => Expr, with no comma. Patterns are a wildcard, a constructor, or a constructor with binders. There are no nested patterns, guards, literal patterns, or as bindings.

Summary#

The reference table is deliberately flat. The semantics of each form are covered in the chapters on values, expressions, types, and contracts. When the table and a chapter disagree, the chapter explains the rule and the table is the quick lookup.