Skip to main content

Operators

Oxide supports all Rust operators plus additional operators for null safety and type checking.

Arithmetic Operators

OperatorDescriptionExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Remaindera % 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:

OperatorDescriptionTranspiles to
i++Postfix increment{ let tmp = i; i += 1; tmp }
++iPrefix increment{ i += 1; i }
i--Postfix decrement{ let tmp = i; i -= 1; tmp }
--iPrefix 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

OperatorDescription
==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

OperatorDescription
&&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

OperatorDescription
&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

OperatorDescription
=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

OperatorDescriptionTranspiles 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

OperatorDescriptionTranspiles to
isType checkPattern match
!isNegative type checkNegated pattern match
asType castas
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

OperatorDescriptionRust Equivalent
a..bHalf-open range [a, b)a..b
a..=bClosed range [a, b]a..=b
a..<bHalf-open (alternative)a..b
..bRange to b..b
a..Range from aa..
..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

OperatorDescriptionRust 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

OperatorDescriptionRust Equivalent
&Immutable reference&
&varMutable 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:

  1. Path operators: ::, .
  2. Unary operators: !, -, *, &, ++, --
  3. Type cast: as, as?, as!
  4. Multiplicative: *, /, %
  5. Additive: +, -
  6. Shift: <<, >>
  7. Bitwise AND: &
  8. Bitwise XOR: ^
  9. Bitwise OR: |
  10. Comparison: ==, !=, <, >, <=, >=
  11. Type check: is, !is
  12. Logical AND: &&
  13. Logical OR: ||
  14. Null coalescing: ??
  15. Range: .., ..=, ..<
  16. 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