rhai/doc/src/language/logic.md

107 lines
2.9 KiB
Markdown
Raw Normal View History

2020-06-20 06:06:17 +02:00
Logic Operators
==============
{{#include ../links.md}}
Comparison Operators
-------------------
Comparing most values of the same data type work out-of-the-box for all [standard types] supported by the system.
However, if using a [raw `Engine`] without loading any [packages], comparisons can only be made between a limited
set of types (see [built-in operators]).
```rust
42 == 42; // true
2020-06-25 05:07:56 +02:00
2020-06-20 06:06:17 +02:00
42 > 42; // false
2020-06-25 05:07:56 +02:00
2020-06-20 06:06:17 +02:00
"hello" > "foo"; // true
2020-06-25 05:07:56 +02:00
2020-06-20 06:06:17 +02:00
"42" == 42; // false
```
Comparing two values of _different_ data types, or of unknown data types, always results in `false`,
except for '`!=`' (not equals) which results in `true`. This is in line with intuition.
```rust
42 == 42.0; // false - i64 cannot be compared with f64
2020-06-25 05:07:56 +02:00
2020-06-20 06:06:17 +02:00
42 != 42.0; // true - i64 cannot be compared with f64
42 > "42"; // false - i64 cannot be compared with string
2020-06-25 05:07:56 +02:00
2020-06-20 06:06:17 +02:00
42 <= "42"; // false - i64 cannot be compared with string
let ts = new_ts(); // custom type
2020-06-25 05:07:56 +02:00
2020-06-20 06:06:17 +02:00
ts == 42; // false - types cannot be compared
2020-06-25 05:07:56 +02:00
2020-06-20 06:06:17 +02:00
ts != 42; // true - types cannot be compared
```
Boolean operators
-----------------
2020-06-21 04:37:17 +02:00
| Operator | Description |
| ----------------- | ------------------------------------- |
| `!` | Boolean _Not_ |
| `&&` | Boolean _And_ (short-circuits) |
| <code>\|\|</code> | Boolean _Or_ (short-circuits) |
| `&` | Boolean _And_ (doesn't short-circuit) |
| <code>\|</code> | Boolean _Or_ (doesn't short-circuit) |
2020-06-20 06:06:17 +02:00
Double boolean operators `&&` and `||` _short-circuit_, meaning that the second operand will not be evaluated
if the first one already proves the condition wrong.
Single boolean operators `&` and `|` always evaluate both operands.
```rust
this() || that(); // that() is not evaluated if this() is true
2020-06-25 05:07:56 +02:00
2020-06-20 06:06:17 +02:00
this() && that(); // that() is not evaluated if this() is false
this() | that(); // both this() and that() are evaluated
2020-06-25 05:07:56 +02:00
2020-06-20 06:06:17 +02:00
this() & that(); // both this() and that() are evaluated
```
Compound Assignment Operators
----------------------------
```rust
2020-06-25 05:07:56 +02:00
let number = 9;
number += 8; // number = number + 8
number -= 7; // number = number - 7
number *= 6; // number = number * 6
number /= 5; // number = number / 5
number %= 4; // number = number % 4
number ~= 3; // number = number ~ 3
2020-06-20 06:06:17 +02:00
number <<= 2; // number = number << 2
2020-06-25 05:07:56 +02:00
2020-06-20 06:06:17 +02:00
number >>= 1; // number = number >> 1
2020-06-25 05:07:56 +02:00
number &= 0x00ff; // number = number & 0x00ff;
number |= 0x00ff; // number = number | 0x00ff;
number ^= 0x00ff; // number = number ^ 0x00ff;
2020-06-20 06:06:17 +02:00
```
The `+=` operator can also be used to build [strings]:
```rust
let my_str = "abc";
my_str += "ABC";
my_str += 12345;
my_str == "abcABC12345"
```