Let's write your first Zith program! This guide gets you up and running in minutes.
zith new hello_world
cd hello_world
# Creates:
# hello_world/
# ZithProject.toml
# src/main.zith
Open src/main.zith and replace the content with:
from std.io.console;
fn main() {
@println("Hello, Zith!");
// Variables use 'let' (immutable) or 'var' (mutable)
let name: string = "Developer";
var age: u32 = 25;
// String interpolation
@println("Welcome, {name}! You are {age} years old.");
// Basic arithmetic
let x: i32 = 10;
let y: i32 = 5;
@println("{x} + {y} = {x + y}");
@println("{x} - {y} = {x - y}");
@println("{x} * {y} = {x * y}");
@println("{x} / {y} = {x / y}");
}
zith run
# Or build and execute separately:
zith build
zith execute
Expected output:
Hello, Zith!
Welcome, Developer! You are 25 years old.
10 + 5 = 15
10 - 5 = 5
10 * 5 = 50
10 / 5 = 2
Functions use implicit returns — the last expression is the return value:
// Implicit return
fn add(a: i32, b: i32): i32 { a + b }
// Void function (no return value)
fn greet(name: string) {
@println("Hello, {name}!");
}
Zith is statically typed with type inference:
// Explicit type annotation
let count: u32 = 42;
let pi: f64 = 3.14159;
let message: string = "Hello";
// Type inference
var inferred = 100; // i32 by default, mutable
let text = "World"; // string
let score: i32 = 85;
if (score >= 90) {
@println("Excellent!");
} else (score >= 70) {
@println("Good job!");
} else {
@println("Keep practicing!");
}
// Ranged for
for (i in 0..10) {
@println("Count: {i}");
}
// Conditional for
for (var i: i32 = 0, i < 10, i += 1) {
@println("Count: {i}");
}
fn main() {
let a: i32 = 20;
let b: i32 = 4;
@println("Sum: {a + b}");
@println("Difference: {a - b}");
@println("Product: {a * b}");
@println("Quotient: {a / b}");
@println("Remainder: {a % b}");
}
fn celsiusToFahrenheit(c: f64): f64 {
(c * 9.0 / 5.0) + 32.0
}
fn main() {
let tempC: f64 = 25.0;
let tempF = celsiusToFahrenheit(tempC);
@println("{tempC}C = {tempF}F");
}
fn factorial(n: u32): u64 {
for ([a,i]: u64 = 1; i in 1..=n) {
a *= i
}
}
fn main() {
let num: u32 = 5;
@println("{num}! = {factorial(num)}");
}
| Command | Description |
|---|---|
zith new <name> |
Create new project |
zith run |
Run project from ZithProject.toml |
zith build |
Build project |
zith check |
Check for errors without building |
zith fmt |
Format code |
zith clean |
Remove build artifacts |