Operators
Oxide supports all Rust operators plus additional operators for null safety and type checking.
Arithmetic Operators
| Operator | Description | Example |
|---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Remainder | a % b |
let sum = 10 + 5 // 15
let diff = 10 - 5 // 5
let product = 10 * 5 // 50
let quotient = 10 / 3 // 3
let remainder = 10 % 3 // 1
Increment/Decrement Operators
Oxide supports C-style increment and decrement:
| Operator | Description | Transpiles to |
|---|---|---|
i++ | Postfix increment | { let tmp = i; i += 1; tmp } |
++i | Prefix increment | { i += 1; i } |
i-- | Postfix decrement | { let tmp = i; i -= 1; tmp } |
--i | Prefix decrement | { i -= 1; i } |
var x = 5
let a = x++ // a = 5, x = 6
let b = ++x // b = 7, x = 7
let c = x-- // c = 7, x = 6
let d = --x // d = 5, x = 5
Comparison Operators
| Operator | Description |
|---|---|
== | Equal |
!= | Not equal |
< | Less than |
> | Greater than |
<= | Less than or equal |
>= | Greater than or equal |
let equal = 5 == 5 // true
let notEqual = 5 != 3 // true
let less = 3 < 5 // true
let greater = 5 > 3 // true
Logical Operators
| Operator | Description |
|---|---|
&& | Logical AND (short-circuit) |
|| | Logical OR (short-circuit) |
! | Logical NOT |
let and = true && false // false
let or = true || false // true
let not = !true // false
Bitwise Operators
| Operator | Description |
|---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
! | Bitwise NOT (on integers) |
<< | Left shift |
>> | Right shift |
let and = 0b1100 & 0b1010 // 0b1000
let or = 0b1100 | 0b1010 // 0b1110
let xor = 0b1100 ^ 0b1010 // 0b0110
let shift = 1 << 4 // 16
Assignment Operators
| Operator | Description |
|---|---|
= | Assignment |
+= | Add and assign |
-= | Subtract and assign |
*= | Multiply and assign |
/= | Divide and assign |
%= | Remainder and assign |
&= | Bitwise AND and assign |
|= | Bitwise OR and assign |
^= | Bitwise XOR and assign |
<<= | Left shift and assign |
>>= | Right shift and assign |
var x = 10
x += 5 // x = 15
x -= 3 // x = 12
x *= 2 // x = 24
Null Safety Operators
| Operator | Description | Transpiles to |
|---|---|---|
!! | Not-null assertion | .unwrap() |
?? | Null coalescing | .unwrap_or() / .unwrap_or_else() |
?. | Safe call | .map(|v| v.method()) |
let value: Int? = 42
// Not-null assertion (panics if null)
let unwrapped = value!!
// Null coalescing (default value)
let result = value ?? 0
// Safe call (propagates null)
let length = name?.len()
See Null Safety for detailed usage.
Type Operators
| Operator | Description | Transpiles to |
|---|---|---|
is | Type check | Pattern match |
!is | Negative type check | Negated pattern match |
as | Type cast | as |
as? | Safe cast | .try_into().ok() |
as! | Force cast | .try_into().unwrap() |
// Type checking
if value is Some(x) {
println!("Got $x")
}
if value !is None {
println!("Has value")
}
// Type casting
let i: Int = 42
let f = i as Float64 // 42.0
// Safe cast (returns Optional)
let result: Int? = anyValue as? Int
// Force cast (panics on failure)
let number: Int = anyValue as! Int
Range Operators
| Operator | Description | Rust Equivalent |
|---|---|---|
a..b | Half-open range [a, b) | a..b |
a..=b | Closed range [a, b] | a..=b |
a..<b | Half-open (alternative) | a..b |
..b | Range to b | ..b |
a.. | Range from a | a.. |
.. | Full range | .. |
for i in 0..5 {
println!(i) // 0, 1, 2, 3, 4
}
for i in 0..=5 {
println!(i) // 0, 1, 2, 3, 4, 5
}
let slice = array[1..4] // Elements 1, 2, 3
let first3 = array[..3] // Elements 0, 1, 2
let last3 = array[-3..] // Last 3 elements
Path Operators
| Operator | Description | Rust Equivalent |
|---|---|---|
. | Field/method access | . |
. | Enum variant (Oxide) | :: |
:: | Path separator | :: |
// Field access
let x = point.x
// Method call
let len = string.len()
// Enum variant (Oxide style)
let status = Status.active
// Path separator
let map = std::collections::HashMap::new()
Note: Oxide allows . for enum variants which transpiles to :::
Status.active // Transpiles to: Status::active
Option.some(42) // Transpiles to: Option::Some(42)
Reference Operators
| Operator | Description | Rust Equivalent |
|---|---|---|
& | Immutable reference | & |
&var | Mutable reference | &mut |
* | Dereference | * |
let x = 42
let ref = &x // Immutable reference
var y = 42
let mutRef = &var y // Mutable reference
let value = *ref // Dereference
Operator Precedence
From highest to lowest:
- Path operators:
::,. - Unary operators:
!,-,*,&,++,-- - Type cast:
as,as?,as! - Multiplicative:
*,/,% - Additive:
+,- - Shift:
<<,>> - Bitwise AND:
& - Bitwise XOR:
^ - Bitwise OR:
| - Comparison:
==,!=,<,>,<=,>= - Type check:
is,!is - Logical AND:
&& - Logical OR:
|| - Null coalescing:
?? - Range:
..,..=,..< - Assignment:
=,+=,-=, etc.
// Precedence examples
let a = 1 + 2 * 3 // 7 (multiplication first)
let b = (1 + 2) * 3 // 9 (parentheses override)
let c = x ?? y || z // x ?? (y || z)
let d = a && b || c // (a && b) || c
See Also
- Null Safety - Null safety operators
- Types - Type casting
- Control Flow - Pattern matching with
is