On this page
Zolo Cheatsheet
Variables¶
let x = 10 // immutable
let mut y = 0 // mutable
const PI = 3.14 // constantTypes¶
int f64 str bool nil
[int] // array
{str: int} // map
(int, str) // tuple
int? // optional
fn(int) -> int // function type
Map<str, int> // genericFunctions¶
fn add(a: int, b: int) -> int { a + b }
let double = |x| x * 2 // lambda
fn apply(f: fn(int) -> int, x: int) -> int { f(x) }Control Flow¶
if cond { } else if cond2 { } else { }
let x = if cond { a } else { b } // if expression
if let Some(v) = opt { use(v) } // if let
match val { 0 => "zero", _ => "other" }Loops¶
for i in 0..10 { } // exclusive range
for i in 0..=10 { } // inclusive range
for item in list { } // iterate
for (i, v) in pairs { } // destructure
while cond { }
while let Some(x) = iter() { } // while let
loop { break }Structs¶
struct Point { x: f64, y: f64 }
let p = Point { x: 1.0, y: 2.0 }
impl Point { fn dist(self) -> f64 { ... } }Enums¶
enum Color { Red, Green, Blue }
enum Shape { Circle(f64), Rect(f64, f64) }
let s = Shape::Circle(5.0)Traits¶
trait Display { fn show(self) -> str }
impl Display for Point { fn show(self) -> str { "..." } }Pattern Matching¶
match value {
0 => "zero",
n if n < 0 => "negative",
Shape::Circle(r) => "circle {r}",
Point { x, y } => "({x},{y})",
'a' | 'b' => "ab",
_ => "other",
}Operators¶
// Arithmetic: + - * / % **
// Comparison: == != < > <= >=
// Logical: && || !
// Assignment: = += -= *= /= %=
// Pipe: |> a |> f() → f(a)
// Tap: &. a &. f() → f(a); a
// Optional: ?. a?.b → nil if a is nil
// Coalesce: ?? a ?? b → b if a is nil
// Propagate: ? expr? → early return on error
// Spread: ... [...a, ...b]
// Range: .. 0..10 (exclusive)
// Range incl: ..= 0..=10 (inclusive)Strings¶
"Hello, {name}!" // interpolation
"""multiline""" // multiline
sql"SELECT * FROM t WHERE id={x}" // tagged templateDecorators¶
@test fn test_it() { ... } // test function
@memoize fn fib(n) { ... } // cached results
@deprecated("msg") fn old() { } // deprecation warning
@builder struct Config { ... } // builder patternStandard Library¶
// String
string.trim(s) string.split(s, sep) string.contains(s, sub)
// Array
Array.map(a, f) Array.filter(a, f) Array.reduce(a, f, init)
Array.push(a, v) Array.len(a) Array.sort(a)
// Map
Map.new() Map.set(m, k, v) Map.get(m, k) Map.keys(m)
// Set
Set.from(arr) Set.add(s, v) Set.has(s, v) Set.union(a, b)
// Option
Option.Some(v) Option.None() Option.unwrap_or(o, def)
// Result
Result.Ok(v) Result.Err(e) Result.unwrap(r)
// Iterator
Iter.from(a) |> Iter.map(f) |> Iter.filter(f) |> Iter.collect()CLI¶
zolo run file.zolo # run
zolo compile file.zolo # show Lua output
zolo check file.zolo # type check only
zolo test file.zolo # run @test functions
zolo fmt file.zolo # format
zolo repl # interactive REPLLua Comparison¶
| Zolo | Lua |
|---|---|
let x = 10 |
local x = 10 |
fn f() { } |
function f() end |
|x| x * 2 |
function(x) return x*2 end |
arr[0] |
arr[1] |
if c { } |
if c then end |
a |> f() |
f(a) |
struct S { } |
table + metatable |
enum E { A(x) } |
{__tag="A", x} |