Operators and Punctuation
Operators and punctuation are the symbol tokens of Rux. The lexer reads them by the longest-match rule, so >>= is one token, not >> followed by =. This page lists every symbol and what it means; how operators bind is in the precedence table.
Arithmetic
| Token | Meaning | Rules |
|---|---|---|
+ | Addition | Arithmetic |
- | Subtraction; prefix negation | Arithmetic |
* | Multiplication; prefix dereference of a pointer; pointer type *T | Arithmetic, Pointers |
/ | Division | Arithmetic |
% | Remainder | Arithmetic |
++ | Increment, prefix or postfix | Arithmetic |
-- | Decrement, prefix or postfix | Arithmetic |
There is no unary + and no power operator: a ** p is a * *p, a multiplication by the value p points to.
Comparison
| Token | Meaning |
|---|---|
== | Equal |
!= | Not equal |
< | Less than |
<= | Less than or equal |
> | Greater than |
>= | Greater than or equal |
See Comparison.
Logical
| Token | Meaning |
|---|---|
&& | Logical and, short-circuiting |
|| | Logical or, short-circuiting |
! | Logical not; with a type, ! E marks a fallible |
See Logical and, for T ! E, Errors.
Bitwise and shift
| Token | Meaning |
|---|---|
& | Bitwise and; in a type, a reference &T |
| | Bitwise or; in a type, a sum A | B |
^ | Bitwise exclusive or |
~ | Bitwise not; before a type name in extend, a destructor func ~T |
<< | Shift left |
>> | Shift right: arithmetic for signed operands, logical for unsigned |
>>> | Logical shift right, filling with zeros |
See Bitwise, Shift, References, Sums and Destructors. Prefix & is not an expression operator; the address of a place is taken with @.
Assignment
| Token | Meaning | Token | Meaning |
|---|---|---|---|
= | Assign | &= | x = x & y |
<- | Move; prefix <-x moves x | |= | x = x | y |
+= | x = x + y | ^= | x = x ^ y |
-= | x = x - y | <<= | x = x << y |
*= | x = x * y | >>= | x = x >> y |
/= | x = x / y | >>>= | x = x >>> y |
%= | x = x % y |
See Assignment and Copy and Move.
Optionals, errors and conditions
| Token | Meaning |
|---|---|
? | After a type, an optional T?. Touching an expression, postfix propagation x?. With whitespace before it, the conditional c ? a : b |
?? | Coalescing, x ?? fallback. In a type, two optional levels T?? |
? is the one token whose meaning depends on whitespace. value? propagates and flag ? a : b selects, so flag? a : b is a syntax error. ?? is always one token: two propagations in a row are written (x?)?.
See Conditional, Coalescing, Optional propagation and Error propagation.
Ranges
| Token | Meaning |
|---|---|
.. | Half-open range a..b; open ranges a.., ..b, ..; a slice type T[..] |
..= | Inclusive range a..=b, ..=b |
... | Inclusive range in expressions and patterns a...b; spread args...; a variadic parameter T... |
See Ranges, Slices and Parameters.
Punctuation
| Token | Meaning |
|---|---|
( ) | Grouping, calls, parameter lists, tuples |
[ ] | Array literals, indexing, array and slice types |
{ } | Blocks, declaration bodies, struct literals |
, | Separates list items |
; | Ends a statement or declaration |
: | Separates a name from its type or field value; the conditional's : |
:: | Path separator, Io::PrintLine, Color::Red |
. | Member access p.x, tuple index t.0, method call s.Len(); leading . in .Success(v) |
-> | Return type of a function or function type |
=> | Separates a match arm's pattern from its result |
@ | Address of a place, @value, giving a pointer |
# | Attributes #Link(…) and compile-time values #target |
See Pointers, Attributes and Compile-time evaluation.
Operators a type can define
A type can give operators such as +, -, *, == and < a meaning of its own by declaring them in an extend block, and can define indexing with an indexer. ?? is built in and cannot be declared. See Operators and Indexers.
See also
- Expressions — precedence and associativity of every operator
- Token Reference — the lexer's name for each token
- Operators and Precedence — the lessons