Types
Oxide uses familiar type names inspired by Swift's type system while maintaining full compatibility with Rust's type system.
Platform-Native Types
These types have platform-dependent sizes (32-bit or 64-bit depending on the target):
| Oxide | Rust | Description |
|---|---|---|
Int | isize | Platform-native signed integer |
UInt | usize | Platform-native unsigned integer |
let count: Int = 42
let size: UInt = 100
Transpiles to:
let count: isize = 42;
let size: usize = 100;
Sized Integer Types
These types have fixed sizes across all platforms:
| Oxide | Rust | Description |
|---|---|---|
Int8 | i8 | 8-bit signed integer |
Int16 | i16 | 16-bit signed integer |
Int32 | i32 | 32-bit signed integer |
Int64 | i64 | 64-bit signed integer |
Int128 | i128 | 128-bit signed integer |
UInt8 | u8 | 8-bit unsigned integer |
UInt16 | u16 | 16-bit unsigned integer |
UInt32 | u32 | 32-bit unsigned integer |
UInt64 | u64 | 64-bit unsigned integer |
UInt128 | u128 | 128-bit unsigned integer |
let byte: UInt8 = 255
let bigNumber: Int64 = 9_223_372_036_854_775_807
Transpiles to:
let byte: u8 = 255;
let big_number: i64 = 9_223_372_036_854_775_807;
Floating Point Types
| Oxide | Rust | Description |
|---|---|---|
Float32 | f32 | 32-bit floating point |
Float64 | f64 | 64-bit floating point |
let pi: Float64 = 3.14159
let approx: Float32 = 2.5
Transpiles to:
let pi: f64 = 3.14159;
let approx: f32 = 2.5;
Special Types
| Oxide | Rust | Description |
|---|---|---|
Char | char | Unicode character |
Bool | bool | Boolean value |
Unit | () | Unit type (void equivalent) |
Never | ! | Never type (function diverges) |
String | String | UTF-8 string |
let letter: Char = 'A'
let flag: Bool = true
let nothing: Unit = ()
fn diverges(): Never {
panic!("This function never returns")
}
Transpiles to:
let letter: char = 'A';
let flag: bool = true;
let nothing: () = ();
fn diverges() -> ! {
panic!("This function never returns")
}
Nullable Types
Any type can be made nullable by appending ?:
let name: String? = "Alice" // Some("Alice")
let empty: String? = null // None
Transpiles to:
let name: Option<String> = Some("Alice".to_string());
let empty: Option<String> = None;
See Null Safety for more details on working with nullable types.
Generics
Oxide supports generic type parameters for both functions and types.
Generic Functions (Kotlin-style)
Functions place generic parameters before the function name:
fn <T> identity(x: T): T { x }
fn <T, U> pair(a: T, b: U): (T, U) { (a, b) }
fn <T: Clone> duplicate(x: T): (T, T) { (x.clone(), x) }
Transpiles to:
fn identity<T>(x: T) -> T { x }
fn pair<T, U>(a: T, b: U) -> (T, U) { (a, b) }
fn duplicate<T: Clone>(x: T) -> (T, T) { (x.clone(), x) }
Generic Structs (Rust-style)
Structs place generic parameters after the name:
struct Wrapper<T>(value: T)
struct Container<T: Clone>(items: Vec<T>)
Transpiles to:
struct Wrapper<T> { value: T }
struct Container<T: Clone> { items: Vec<T> }
Lifetime Parameters
Oxide supports Rust's lifetime system. For simple cases, lifetimes are inferred automatically:
// Lifetime inferred - no annotation needed
struct Parser(source: &String)
// Explicit lifetime when needed
struct RefHolder<'a, T>(inner: &'a T)
fn <'a> first(items: &'a [Int]): &'a Int {
&items[0]
}
Transpiles to:
struct Parser<'a> { source: &'a String }
struct RefHolder<'a, T> { inner: &'a T }
fn first<'a>(items: &'a [isize]) -> &'a isize {
&items[0]
}
See Ownership - Automatic Lifetime Inference for details on when inference applies.
Const Generics
Const generics enable types parameterized by compile-time values:
struct Buffer<const CAP: UInt>(len: Int)
fn <const N: UInt> getSize(): UInt { N }
Transpiles to:
struct Buffer<const CAP: usize> { len: isize }
fn get_size<const N: usize>() -> usize { N }
See Also
- Variables - Variable declarations
- Functions - Function syntax
- Data Structures - Structs and enums