Keywords and Operators
Keywords
These words are reserved and can't be used as names:
| Keyword | Use |
|---|---|
fn | declare a function or method |
let, mut | declare a variable; make it changeable |
if, else | choose between branches |
match | choose by pattern |
for, in, while | loop |
break, continue | leave a loop; start its next round |
return | leave a function with a value |
struct, enum | declare a type |
interface, dyn | declare an interface; a value of any type that satisfies one |
pub | export an item from its module |
use | bring in a module |
group | run tasks that finish when the block ends |
select | wait for one of several channel operations |
defer | run a statement when the block is left |
true, false | the two Bool values |
These words have a meaning only in their own place (where they start their construct, or after a pattern for as), so they remain usable as names elsewhere (a field can be called type):
| Word | Use |
|---|---|
type | declare a type alias: type Counts = Map[Str, Int] |
test, bench | declare a test or a benchmark |
foreign | declare C functions |
with | copy a struct with changes: { p with x: 1 } |
until | give a group a deadline |
interleaved | let a group's tasks print as they go |
virtual | a test clock on virtual time |
as | in a pattern, also bind the whole value: Some(p) as whole |
Operators
From loosest to tightest binding. All binary operators are left-associative.
| Level | Operators | Meaning |
|---|---|---|
| 1 | |> | pipe: x |> f(a) is f(x, a) |
| 2 | || | or, short-circuit |
| 3 | && | and, short-circuit |
| 4 | == != | equal, not equal (structural) |
| 5 | < <= > >= | compare ordered values |
| 6 | ..< | range: lo..<hi |
| 7 | + - | ^ | add, subtract, bitwise or, exclusive or; + also joins Str, Bytes and lists |
| 8 | * / % & << >> | multiply, divide, remainder, bitwise and, shift left, shift right keeping the sign |
| 9 | ?? | the value in Some or Ok, or a default |
| 10 | - ! ~ | negate, not, flip every bit |
| 11 | f(x) a[i] p.x a?.b e? | call, index, field or method, safe navigation, early return |
Both operands of a binary operator must have the same type: there are no implicit conversions.
The bitwise operators work on Int. Because & binds like * and | like +, x & 1 == 0 is (x & 1) == 0, and a + b << 2 is a + (b << 2). A shift by 64 or more moves every bit out (0, or -1 for >> of a negative number); a negative shift count panics. x.shift_right_unsigned(n) shifts right filling with zeros.
Other symbols
| Symbol | Meaning |
|---|---|
-> | a function's result type |
=> | separates a pattern from its arm in match and select |
|x| expr | a closure |
? after an expression | return early on None or Err |
? "text" | the same, adding context to the error |
?. | safe navigation on an Option |
{ ... } inside a string | interpolation; \{ for a literal brace |
// | a comment, to the end of the line |
_ | a pattern that matches anything; a name for a value you don't need |
p1 | p2 in a pattern | an or-pattern: matches when either alternative does |
.. in a pattern | the items not written out: Request(method, ..), (first, ..), [.., last] |
..name in a list pattern | the items not written out, as a new list: [first, ..rest] |
[p1, p2] in a pattern | a list of exactly those items |
Point { x, y: 0 } in a pattern | a struct, by its fields; x alone binds the field x; .. at the end skips the rest |
p as name in a pattern | what p matches, with the whole value bound to name |
(a, b) = value | assign a tuple's items to existing mut variables |
Error codes
Every compiler diagnostic has a code, such as E0302. tessera explain CODE describes it, and tessera check --json reports codes, positions, notes and automatic fixes for tools. Codes from E0001 are about syntax, from E0101 names and types, from E0201 type checking, from E0301 capabilities and concurrency, from E0320 packages, from E0330 foreign functions, and from E0401 modules. Codes starting with W are warnings.