Hello World
Write, run, and compile your first Turbo program.
Create a Source File
Create a file called hello.tb with the following content:
fn main() {
print("Hello, world!")
}Every Turbo program needs a main function as its entry point. The print function is a built-in that outputs to stdout.
Run with JIT
Use turbo run to compile and execute in a single step using the JIT compiler:
$ turbo run hello.tb
Hello, world!The JIT (Just-In-Time) compiler uses Cranelift to compile your code to machine code in memory and runs it immediately. This is the fastest way to iterate during development.
Build a Native Binary
Use turbo build to compile to a standalone native executable:
$ turbo build hello.tb
$ ./hello
Hello, world!AOT (Ahead-Of-Time) compilation produces a self-contained binary with no runtime dependencies. The binary is linked with a small C runtime for I/O and memory operations.
JIT vs AOT
| Mode | Command | Use Case |
|---|---|---|
| JIT | turbo run | Development, rapid iteration |
| AOT (Cranelift) | turbo build | Production binaries |
| AOT (LLVM) | turbo build --llvm | Maximum optimization |
A Slightly Bigger Example
fn abs(x: i64) -> i64 {
if x < 0 { 0 - x } else { x }
}
fn clamp(val: i64, lo: i64, hi: i64) -> i64 {
if val < lo { lo }
else { if val > hi { hi } else { val } }
}
fn main() {
assert(abs(-42) == 42)
assert(clamp(150, 0, 100) == 100)
print("All checks passed!")
}Notice how if is an expression that returns a value -- no need for ternary operators or explicit return statements.