Standard Library
Oxide's standard library provides a minimal set of type aliases and leverages Rust's standard library methods directly. Oxide's case conversion automatically translates camelCase method calls to Rust's snake_case equivalents.
Design Philosophy
Unlike traditional standard libraries with custom extension traits, Oxide takes a simpler approach:
- Type aliases -
Array<T>forVec<T>,Dict<K, V>forHashMap<K, V> - Direct Rust methods - Use Rust's standard library methods with camelCase naming
- Automatic case conversion -
toUppercase()becomesto_uppercase(),isEmpty()becomesis_empty()
This means you can use any Rust method - just write it in camelCase and Oxide handles the conversion.
Automatic Import
The stdlib prelude is automatically imported in every Oxide file:
// These are available immediately
let arr: Array<Int> = [1, 2, 3]
let map: Dict<String, Int> = Dict.new()
To disable the prelude:
@file:[no_prelude]
// Must manually import what you need
Prelude Contents
The prelude automatically imports:
| Type | Wraps | Description |
|---|---|---|
Array<T> | Vec<T> | Dynamic array type alias |
Dict<K, V> | HashMap<K, V> | Hash map type alias |
Quick Reference
String Operations
Strings use Rust's standard methods with camelCase naming:
let s = " Hello, World! "
s.trim() // "Hello, World!"
s.toUppercase() // " HELLO, WORLD! "
s.toLowercase() // " hello, world! "
s.isEmpty() // false
s.startsWith(" H") // true
s.contains("World") // true
s.replace("World", "Oxide") // " Hello, Oxide! "
Option Operations
Options use Rust's standard methods:
let opt: Int? = 42
opt != null // true (idiomatic null check)
opt ?? 0 // 42 (null coalescing)
opt.map { it * 2 } // Some(84)
opt.filter { it > 10 } // Some(42)
opt.andThen { it + 1 } // Some(43)
Result Operations
Results use Rust's standard methods:
let result: Result<Int, String> = Ok(42)
result.isOk() // true
result.unwrapOr(0) // 42
result.map { it * 2 } // Ok(84)
Collection Operations
Arrays provide both Rust methods and convenience methods:
var arr = [3, 1, 4, 1, 5]
arr.isEmpty() // false
arr.first() // Some(&3)
arr.len() // 5
arr.sorted() // [1, 1, 3, 4, 5] (returns new array)
arr.reversed() // [5, 1, 4, 1, 3] (returns new array)
// Mutating
arr.append(9) // adds to end
arr.removeLast() // removes and returns last
arr.insertAt(0, 0) // inserts at index
arr.removeAt(0) // removes at index
Iterator Operations
Iterators use Rust's standard methods plus some convenience aliases:
let numbers = [1, 2, 3, 4, 5]
numbers.iter()
.filter { it % 2 == 0 }
.map { it * 2 }
.toArray() // [4, 8]
// Also available
numbers.iter().taken(3).toArray() // first 3 elements
numbers.iter().skipped(2).toArray() // skip first 2
numbers.iter().findFirst { it > 3 } // first match
Math Operations
Math operations are methods on numeric types:
let n: Int = -7
n.abs() // 7
n.min(10) // -7
n.max(10) // 10
n.clamp(0, 5) // 0
let f: Float64 = 16.0
f.sqrt() // 4.0
f.floor() // 16.0
f.ceil() // 16.0
f.round() // 16.0
let two: Int = 2
two.pow(10) // 1024
// Even/odd checks use modulo
6 % 2 == 0 // true (even)
7 % 2 != 0 // true (odd)
I/O Operations
// File I/O
let content = try? readFile("data.txt")
try writeFile("output.txt", "Hello!")
// Console I/O
print!("Enter name: ")
let name = try? readLine() ?? "Guest"
Type Aliases
The stdlib provides wrapper types for cleaner naming:
| Oxide Type | Wraps | Description |
|---|---|---|
Array<T> | Vec<T> | Dynamic array |
Dict<K, V> | HashMap<K, V> | Hash map |
These are simple type aliases with zero runtime overhead.
Naming Conventions
Oxide uses camelCase for method names, which are automatically converted to Rust's snake_case:
| Oxide | Rust |
|---|---|
isEmpty() | is_empty() |
toUppercase() | to_uppercase() |
toLowercase() | to_lowercase() |
startsWith(p) | starts_with(p) |
endsWith(s) | ends_with(s) |
unwrapOr(d) | unwrap_or(d) |
unwrapOrElse(f) | unwrap_or_else(f) |
andThen(f) | and_then(f) |
containsKey(k) | contains_key(k) |