Examples

Explore practical Zolo language examples. Click any example to open in the interactive playground.

12 examples 7 categories
hello.zolo
Basic
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
variables.zolo
Basic
// 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
pipe.zolo
Basic
// 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
pattern-matching.zolo
Control Flow
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
iterators.zolo
Iterators
// 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]
structs.zolo
Structures
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)
enums.zolo
Structures
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
null-safety.zolo
Safety
struct Address {
    street: str,
    city: str?,
}

struct User {
    name: str,
    address: Address?,
}

let user = User {
    name: "Alice",
...

// output

Alice: São Paulo
Guest: Desconhecida
decorators.zolo
Advanced
// @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.zolo
Functions
// 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
array-ops.zolo
Structures
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.zolo
Advanced
// 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 →
enespt-br