Skip to main content

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:

  1. Type aliases - Array<T> for Vec<T>, Dict<K, V> for HashMap<K, V>
  2. Direct Rust methods - Use Rust's standard library methods with camelCase naming
  3. Automatic case conversion - toUppercase() becomes to_uppercase(), isEmpty() becomes is_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:

TypeWrapsDescription
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! "

Full String Reference

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)

Full Option Reference

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)

Full Result Reference

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

Full Collections Reference

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

Full Iterator Reference

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)

Full Math Reference

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"

Full I/O Reference

Type Aliases

The stdlib provides wrapper types for cleaner naming:

Oxide TypeWrapsDescription
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:

OxideRust
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)

See Also