2020-06-20 06:06:17 +02:00
|
|
|
Numeric Operators
|
|
|
|
=================
|
|
|
|
|
|
|
|
{{#include ../links.md}}
|
|
|
|
|
|
|
|
Numeric operators generally follow C styles.
|
|
|
|
|
|
|
|
Unary Operators
|
|
|
|
---------------
|
|
|
|
|
|
|
|
| Operator | Description |
|
|
|
|
| -------- | ----------- |
|
2020-09-24 05:17:39 +02:00
|
|
|
| `+` | positive |
|
|
|
|
| `-` | negative |
|
2020-06-20 06:06:17 +02:00
|
|
|
|
|
|
|
```rust
|
|
|
|
let number = -5;
|
|
|
|
|
|
|
|
number = -5 - +5;
|
|
|
|
```
|
|
|
|
|
|
|
|
Binary Operators
|
|
|
|
----------------
|
|
|
|
|
2020-06-22 16:02:49 +02:00
|
|
|
| Operator | Description | Integers only |
|
|
|
|
| --------------- | ---------------------------------------------------- | :-----------: |
|
2020-09-24 05:17:39 +02:00
|
|
|
| `+` | plus | |
|
|
|
|
| `-` | minus | |
|
|
|
|
| `*` | multiply | |
|
|
|
|
| `/` | divide (integer division if acting on integer types) | |
|
|
|
|
| `%` | modulo (remainder) | |
|
|
|
|
| `~` | power | |
|
|
|
|
| `&` | bit-wise _And_ | Yes |
|
|
|
|
| <code>\|</code> | bit-wise _Or_ | Yes |
|
|
|
|
| `^` | bit-wise _Xor_ | Yes |
|
|
|
|
| `<<` | left bit-shift | Yes |
|
|
|
|
| `>>` | right bit-shift | Yes |
|
2020-06-20 06:06:17 +02:00
|
|
|
|
|
|
|
```rust
|
|
|
|
let x = (1 + 2) * (6 - 4) / 2; // arithmetic, with parentheses
|
|
|
|
|
|
|
|
let reminder = 42 % 10; // modulo
|
|
|
|
|
|
|
|
let power = 42 ~ 2; // power (i64 and f64 only)
|
|
|
|
|
|
|
|
let left_shifted = 42 << 3; // left shift
|
|
|
|
|
|
|
|
let right_shifted = 42 >> 3; // right shift
|
|
|
|
|
|
|
|
let bit_op = 42 | 99; // bit masking
|
|
|
|
```
|