Quick Reference
Constructs
| Concept | Syntax |
|---|---|
| Immutable binding | let x = 42 |
| Mutable binding | var x = 42 |
| Type annotation | let x: i32 = 42 |
| Function | fn name(a: i32): i32 { ... } |
| Generic function | fn name<T>(x: T): T { ... } |
| Generic bound | <T: Eq + Hash> |
| Reference param | fn f(x: &T) is called f(x); fn f(x: &mut T) is called f(&mut x) |
| Struct | struct Name { field: Type } |
| Enum | enum Name { Variant(Type), Empty } |
| Match | match val { Name.Variant(x) => { ... } } |
| If let | if let Some(x) = opt { ... } |
| Vec | var v: Vec<i32> = Vec.new() |
| HashMap | var m: HashMap<K, V> = HashMap.new() |
| Heap | Heap(value), deref with *heaped |
| Import | from "path" import { A, B } |
| FFI | extern fn name(args): ret |
| Trait | trait Name { fn method(self: &Self): T } |
| Impl trait | impl Trait for Type { ... } |
| Impl methods | impl Type { ... } |
| Cast | expr as Type |
| JSON serialize | jsonStringify(struct_val) |
| Number to string | n.toString() |
| Hex / binary literal | 0xFF, 0b1010 |
For every reserved word see Keywords; for @ attributes see Annotations & builtins.
Operators and punctuation
Generated from the compiler's token table by scripts/gen-lang-docs.ts.
| Token | Meaning |
|---|---|
( | Grouping, a call's arguments, a parameter list. |
) | Closes (. |
{ | A block, a struct literal, a declaration body. |
} | Closes {. |
[ | An array literal [1, 2, 3] or [0; 100], an index v[i], a slice s[a..b]. |
] | Closes [. |
: | Type annotation let x: i32, a parameter's type, a return type fn f(): i32, a struct field Point { x: 1 }. |
; | Optional statement separator; the count in an array repeat [0; 100]. |
, | Separates arguments, parameters, fields, elements. |
. | Field access, method call, and paths: Option.Some, Vec.new(). |
-> | Not used by the grammar: a return type follows :. |
* | Multiply; a raw pointer type *T; deref *p (unsafe for a raw pointer). |
+ | Add; string concatenation. |
- | Subtract; negate. |
/ | Divide. |
% | Remainder. |
& | Bitwise and; a reference parameter type &T / &mut T; the &mut x argument marker. &x as an expression is an error: shared borrows are implicit, f(x). |
&& | Logical and (short-circuit). |
|| | Logical or (short-circuit). |
= | Assignment and binding. |
== | Equal. |
=> | A match arm Pat => expr; a closure body (x: i32) => x * 2. |
:: | Not used by the grammar: paths use ., as in Option.Some. |
!= | Not equal. |
< | Less than; opens type arguments Vec<T>; two adjacent, <<, shift left. |
> | Greater than; closes type arguments; two adjacent, >>, shift right. |
<= | Less than or equal. |
>= | Greater than or equal. |
! | Logical not; postfix unwrap expr! (traps on None or Err); @!wrapping applies an attribute to the whole file. |
.. | Range 0..n (end excluded), a slice s[a..b], a range-constrained integer type i32(0..100). |
... | Variadic tail of an extern fn parameter list. |
? | Postfix propagate expr? (returns None or Err early); T? is Option<T>; ?&T is the nullable extern reference. |
?? | Default: opt ?? fallback. |
| | Bitwise or. |
^ | Bitwise xor. |
~ | Bitwise not. |
@ | An attribute @derive(Eq), or a builtin sigil @embedFile("path"). |
+= | Add and assign. |
-= | Subtract and assign. |
*= | Multiply and assign. |
/= | Divide and assign. |
%= | Remainder and assign. |
&= | Bitwise and, and assign. |
|= | Bitwise or, and assign. |
^= | Bitwise xor, and assign. |
Primitive types
Width, signedness and aliases as the type checker resolves them.
| Type | Kind | Bits | Note |
|---|---|---|---|
i8 | signed integer | 8 | |
i16 | signed integer | 16 | |
i32 | signed integer | 32 | |
i64 | signed integer | 64 | |
int | signed integer | 64 | alias of i64 |
u8 | unsigned integer | 8 | |
byte | unsigned integer | 8 | alias of u8 |
u16 | unsigned integer | 16 | |
u32 | unsigned integer | 32 | |
u64 | unsigned integer | 64 | |
f32 | floating point | 32 | |
f64 | floating point | 64 | |
float | floating point | 64 | alias of f64 |
bool | true or false | ||
void | no value | ||
string | owned UTF-8 string |