Quick Start
Get started with Mozhi in 5 minutes. This guide assumes you've already installed the Mozhi toolchain.
Your First Program
Create a file called hello.mz:
# hello.mz
echo("Hello, World!")
Run it:
$ mozhi run hello.mz
Hello, World!
Variables and Types
# Variables are declared by assignment
name = "Mozhi"
age = 20
price = 99.99
active = true
# Constants use the const keyword
const PI = 3.14159
# Explicit type annotations (optional)
count: int = 0
label: string = "default"
echo(name, " is ", age, " years old")
Functions
# Named function
fn add(a, b) {
return a + b
}
# Function with type annotations
fn greet(name: string) -> string {
return "Hello, " + name
}
# Anonymous function (lambda)
double = fn(x) { return x * 2 }
echo(add(3, 4)) # 7
echo(greet("Alice")) # Hello, Alice
echo(double(5)) # 10
Control Flow
# If / else
if age >= 18 {
echo("Adult")
} else {
echo("Minor")
}
# While loop
i = 0
while i < 5 {
echo("i = ", i)
i += 1
}
# For loop (C-style)
for j = 0; j < 3; j += 1 {
echo("j = ", j)
}
# For-in loop
fruits = ["apple", "banana", "cherry"]
for fruit in fruits {
echo(fruit)
}
# Match (pattern matching)
x = 2
match x {
1 => echo("One")
2 => echo("Two")
3 => echo("Three")
_ => echo("Other")
}
Structs and Traits
# Define a struct
struct User {
name: string
age: int
}
# Define a trait
trait Greetable {
fn greet()
}
# Implement the trait for User
impl Greetable for User {
fn greet() {
echo("Hi, I'm ", name)
}
}
# Create an instance
user = User { name: "Alice", age: 30 }
user.greet() # Hi, I'm Alice
Arrays and Strings
# Arrays
nums = [1, 2, 3, 4, 5]
echo(len(nums)) # 5
echo(nums[0]) # 1
nums.push(6) # add element
last = nums.pop() # remove and return last
# Strings
name = "Mozhi"
greeting = "Hello, " + name
echo(len(name)) # 4
Error Handling
fn divide(a, b) {
if b == 0 {
throw Error { message: "Division by zero" }
}
return a / b
}
try {
result = divide(10, 0)
} catch err {
echo("Error: ", err.message)
}
Imports & Libraries (v2.3+)
# Three import styles:
import html # wildcard (func() directly)
import http from "mozhi-http" # namespace (http.func())
import my from "mylib.mz" # file path (my.func())
# Libraries auto-download from GitHub on first use!
page = html_page("My Site", html_h1("Hello!"))
echo(page)
File I/O & Shell (v2.4+)
# File operations
write_file("data.txt", "Hello!")
content = read_file("data.txt")
echo(content)
# Run shell commands
output = run("ls -la")
echo(output)
# Run other language files
echo(run_c("program.c")) # compile & run C
echo(run_py("script.py")) # run Python
echo(run_sh("script.sh")) # run shell script
HTTP Server (v2.4+)
import http from "mozhi-http"
fn handler(method, path) {
# Serve static files from public/ directory
return http.http_serve_static(path, "public")
}
server = http.http_start(8080)
http.http_serve_loop(server, handler)
REPL
Start the interactive REPL:
$ mozhi
Mozhi REPL
Version 2.0
> x = 10
> echo(x)
10
> fn double(n) { return n * 2 }
> double(5)
10
> exit
Next Steps
- Read the full Language Specification
- Explore example programs
- Learn about the Package Manager
- Join the community on GitHub Issues