Examples
Explore practical Zolo language examples. Click any example to open in the interactive playground.
12 examples 7 categories
fn main() {
let name = "Zolo"
let version = 1
print("Hello, {name} v{version}!")
print("A linguagem moderna com VM em Rust")
}
main() // output
Hello, Zolo v1! A linguagem moderna com VM em Rust
// let: imutável, const: constante, mut: mutável
let name: str = "Zolo"
const PI: float = 3.14159
let mut counter = 0
counter = counter + 1
counter = counter + 1
print("Nome: {name}")
print("PI: {PI}")
print("Contador: {counter}")
... // output
Nome: Zolo PI: 3.14159
// O pipe |> passa o resultado como primeiro argumento
fn double(x: int) -> int { x * 2 }
fn add_ten(x: int) -> int { x + 10 }
fn square(x: int) -> int { x * x }
let result = 5
|> double()
|> add_ten()
|> square()
print(result) // ((5*2)+10)^2 = 400
... // output
400 Hello, World
fn classify(n: int) -> str {
match n {
0 => "zero",
1..=9 => "single digit",
10..=99 => "double digit",
_ => "large",
}
}
fn day_name(d: int) -> str {
match d {
1 => "Segunda",
... // output
zero single digit
// Iteradores lazy — só calculam o que é necessário
// Quadrados pares dos primeiros 5 números
let result = 0..
|> Iter.map(|x| x * x)
|> Iter.filter(|x| x % 2 == 0)
|> Iter.take(5)
|> Iter.collect()
print(result)
// Fibonacci via Iter.from_fn
... // output
[0, 4, 16, 36, 64] [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
struct Point {
x: float,
y: float,
}
impl Point {
fn new(x: float, y: float) -> Point {
return Point { x, y }
}
fn distance(self, other: Point) -> float {
let dx = self.x - other.x
... // output
A: (0.0, 0.0) B: (3.0, 4.0)
enum Shape {
Circle(float),
Rect(float, float),
Triangle(float, float, float),
}
fn area(shape: Shape) -> float {
match shape {
Shape.Circle(r) => 3.14159 * r * r,
Shape.Rect(w, h) => w * h,
Shape.Triangle(a, b, c) => {
let s = (a + b + c) / 2.0
... // output
Círculo com raio 5.0: área = 78.53975 Retângulo 4.0x6.0: área = 24.0
struct Address {
street: str,
city: str?,
}
struct User {
name: str,
address: Address?,
}
let user = User {
name: "Alice",
... // output
Alice: São Paulo Guest: Desconhecida
// @memoize: cache automático de resultados
@memoize
fn fibonacci(n: int) -> int {
if n <= 1 { return n }
return fibonacci(n - 1) + fibonacci(n - 2)
}
// @test: testes inline
@test
fn test_fibonacci() {
assert_eq(fibonacci(0), 0)
assert_eq(fibonacci(1), 1)
... // output
fib(30) = 832040 fib(35) = 9227465
// Closures capturam variáveis do escopo externo
fn make_counter(start: int) {
let mut count = start
return || {
count = count + 1
return count
}
}
let counter = make_counter(0)
print(counter()) // 1
print(counter()) // 2
... // output
1 2
let nums = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3]
// Operações básicas
print("Length: {Array.len(nums)}")
print("First: {nums[0]}")
print("Last: {nums[Array.len(nums) - 1]}")
// Funcionais
let evens = nums |> Array.filter(|x| x % 2 == 0)
let doubled = nums |> Array.map(|x| x * 2)
let sum = nums |> Array.reduce(|acc, x| acc + x, 0)
... // output
Length: 10 First: 3
// Generators com fn* e yield
fn* range(start: int, end: int, step: int) {
let mut i = start
while i < end {
yield i
i = i + step
}
}
fn* fibonacci() {
let mut a = 0
let mut b = 1
... // output
0 3
Want to learn step by step?
The interactive Tutorial guides you through the language with practical exercises and immediate feedback.
Start the Tutorial →