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 |
+% | Wrapping addition | Arithmetic |
-% | Wrapping subtraction | Arithmetic |
*% | Wrapping multiplication | Arithmetic |
++ | Increment, prefix or postfix | Arithmetic |
-- | Decrement, prefix or postfix | Arithmetic |
+, -, *, negation, ++ and -- stop the program when an integer result does not fit its type; +%, -% and *% keep the two's-complement result instead. A prefix * reads through a raw pointer, which needs an unsafe context. 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 |
See Logical. ! is not a type operator: the old fallible spelling T ! E is rejected in favour of -> T throws E; see Errors.
Bitwise and shift
| Token | Meaning |
|---|---|
& | Bitwise and; in a type, a reference &T |
| | Bitwise or; in a type, a sum A | B; in a pattern, alternatives |
^ | 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 |
+= | 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 |
+%= | x = x +% y | -%= | x = x -% y |
*%= | x = x *% y |
A transfer of ownership is the keyword move, as in target = move source. The old arrow <- is still read as one token so that it can be reported: x <- y; is error: '<-' is not an assignment operator with help: write 'target = move source'.
See Assignment and Copy and Move.
Optionals
| Token | Meaning |
|---|---|
? | After a type, an optional T?. Touching an expression, postfix propagation of an optional's absence x? |
?? | Coalescing, x ?? fallback. In a type, two optional levels T?? |
? is the one token whose meaning depends on whitespace. value? propagates; a ? with whitespace before it would start the conditional operator c ? a : b, which Rux does not have — it is error: the conditional operator 'c ? a : b' is not supported with help: write 'if c { a } else { b }'. ? propagates only absence, never an error: a call that can throw is marked with try. ?? is always one token: two propagations in a row are written (x?)?. try? is the keyword try followed by ?.
See Conditional values, Coalescing, Optional propagation and Errors.
Ranges
| Token | Meaning |
|---|---|
.. | Half-open range a..b; open ranges a.., ..b, ..; a slice type T[..] |
..= | Inclusive range a..=b, ..=b |
... | Spread args...; a variadic parameter T... — never a range |
..= is the only inclusive spelling: 1...5 is error: '...' is not a range operator with help: use '..='. 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, field value or argument label |
:: | Path separator, Io::PrintLine, Color::Red |
. | Member access p.x, tuple index t.0, method call s.Len(); leading . in .Some(v) and .Red |
-> | Return type of a function or function type |
=> | Separates the pattern of a match, catch or when arm 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