C-Style Clarity
A familiar C-style mental model with explicit locals, explicit loops, and direct control over values and branches.
fn sumRange(limit: s32) s32 {
var i: s32 = 0;
var total: s32 = 0;
while i < limit {
total += i;
i++;
}
return total;
}
fn main() s32 @public {
var total: s32 = sumRange(5);
if total != 10 {
return 1;
}
return 0;
}
struct Buffer @public {
data: ptr[u8],
length: usize,
capacity: usize
}
fn main() s32 @public {
var buffer: Buffer = new Buffer {
data: nullptr,
length: 8,
capacity: 64
};
buffer->length = buffer->length + 4;
return buffer->capacity as s32;
}
Generics at Compile Time
Generic functions and structures with explicit instantiation and no runtime generic overhead.
fn add[T](a: T, b: T) T @public {
return a + b;
}
struct Pair[T] @public {
left: T,
right: T
}
fn main() s32 @public {
var nums := new Pair[s32] {
left: 2,
right: 4
};
return nums->left + nums->right;
}
struct Buffer[T] @public {
data: ptr[T],
length: usize,
capacity: usize
}
fn main() s32 @public {
var buf := new Buffer[u8] {
data: nullptr,
length: 0,
capacity: 64
};
return buf->capacity as s32;
}
C Interoperability
Direct interop with C code via @extern and @convention("C").
fn printf(fmt: const array[char]) s32
@public @arbitraryArgs
@extern("printf") @convention("C");
fn atoi(text: const array[char]) s32
@public
@extern("atoi") @convention("C");
fn puts(text: const array[char]) s32
@public
@extern("puts") @convention("C");
fn main() s32 @public {
if puts("interop ready") < 0 {
return 1;
}
return 0;
}
Compile-Time Conditionals
Use @if, @elif, and @else to select platform-specific code at compile time.
@if(isLinux()) const PLATFORM: u32 = 2;
@elif(isWindows()) const PLATFORM: u32 = 1;
@else const PLATFORM: u32 = 3;
fn main() s32 @public {
var count: s32 = 0;
@if(false) {
count += 1;
} @elif(true) {
count += 2;
} @else {
count += 100;
}
return count;
}
Useful Imports
Mix std:: modules, aliases, and selective imports to keep dependencies explicit without making the file noisy.
import std::io;
import std::math;
import std::collections::vector;
fn main() s32 @public {
var values := vector::newVector[s32]();
var angle: f64 = math::PI_4;
vector::pushBack[s32](ref values, 10);
vector::pushBack[s32](ref values, 20);
io::print("last=%d cos=%f\n", vector::back[s32](ref values), math::cosine(angle));
vector::destroyVector[s32](ref values);
return 0;
}
import "other.thrust" as dep;
import "other.thrust" only { stack, counter };
import module_a::sub::module_b;
fn main() s32 @public {
var value: module_a::sub::module_b::MyType = 0;
if counter > 0 {
return value as s32;
}
return 0;
}
Built-in Compile Helpers
Type/layout builtins like sizeOf, alignOf, and isSameType for safer code.
const U32_SIZE: usize = sizeOf(u32);
const U32_ALIGN: u32 = alignOf(u32);
const U32_BITS: usize = typeWidth(u32);
const PTR_BITS: usize = pointerWidth();
const COMPILER_LEN: usize = stringLength(compilerVersion());
fn verifyTypes() void {
staticAssert(isSameType(u32, u32), "u32 type check failed");
}
struct Header @public {
magic: u32,
count: u16,
flags: u16
}
const HEADER_SIZE: usize = sizeOf(Header);
const HEADER_ALIGN: u32 = alignOf(Header);
fn validateLayout() void {
staticAssert(HEADER_SIZE >= 8, "Header layout is too small");
staticAssert(HEADER_ALIGN >= 2, "Header alignment is too small");
}