27 · Biblioteca Padrão
Vetores (std::array)
Mutação, transformação, filtragem, agregação, busca e achatamento de arrays.
Nesta página
Arrays em Zolo são sequências de comprimento variável. Declare com let mut
para obter um array mutável. Os métodos estão disponíveis na forma de método
(arr.map(...)) e na forma de módulo (Array::map(arr, ...)). Importe com
use std::Array quando usar a forma de módulo.
Empilhar e desempilhar
push acrescenta ao final; pop remove e retorna o último elemento — o padrão
clássico de pilha LIFO.
Construção incremental e pilha LIFO com push/pop.
// Feature: Array push / pop — mutation at the end of the array
// When to use: building arrays incrementally, using as a stack.
use std::Array
// `let mut` allows mutating the array.
let mut nums = [1, 2, 3]
// Push appends at the end.
nums.push(4)
nums.push(5)
print(nums)
// expected: [1, 2, 3, 4, 5]
print(nums.len()) // expected: 5
// Pop removes and returns the last element.
let last = nums.pop()
print(last) // expected: 5
print(nums) // expected: [1, 2, 3, 4]
// Classic LIFO stack.
let mut stack = []
stack.push("a")
stack.push("b")
stack.push("c")
print(stack.pop()) // expected: c
print(stack.pop()) // expected: b
print(stack.len()) // expected: 1
// Equivalent module form.
let mut xs = [10]
xs.push(20)
xs.push(30)
print(xs)
// expected: [10, 20, 30]
Transformação com map
map aplica uma função a cada elemento e retorna um novo array de mesmo
comprimento. O array original não é modificado.
Dobrar, elevar ao quadrado, converter tipo, transformar strings — map é sempre não-destrutivo.
// Feature: Array.map — transforms each element while preserving length
// When to use: applying the same transformation to a whole collection.
use std::Array
let nums = [1, 2, 3, 4, 5]
// Double each number.
let doubled = nums.map(|x| x * 2)
print(doubled)
// expected: [2, 4, 6, 8, 10]
// Square.
let squares = nums.map(|x| x * x)
print(squares)
// expected: [1, 4, 9, 16, 25]
// Type conversion: int -> str.
let labels = Array::map([1, 2, 3], |n| "item-{n}")
print(labels[0]) // expected: item-1
print(labels[2]) // expected: item-3
// Map over strings.
let names = ["alice", "bob", "carol"]
let upper = names.map(|s| s.upper())
print(upper)
// expected: [ALICE, BOB, CAROL]
// Original untouched.
print(nums)
// expected: [1, 2, 3, 4, 5]
Filtragem com filter
filter mantém apenas os elementos que satisfazem o predicado.
Pares, maiores que 5, strings não-vazias; composição filter + map.
// Feature: Array.filter — keeps only elements that satisfy a predicate
// When to use: selecting a subset, removing invalid items.
use std::Array
let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
// Even numbers.
let evens = nums.filter(|x| x % 2 == 0)
print(evens)
// expected: [2, 4, 6, 8, 10]
// Greater than 5.
let big = nums.filter(|x| x > 5)
print(big)
// expected: [6, 7, 8, 9, 10]
// Filter non-empty strings.
let words = ["foo", "", "bar", "", "baz"]
let nonempty = words.filter(|s| s.len() > 0)
print(nonempty)
// expected: [foo, bar, baz]
// Compose with map — double only the evens.
let just_evens = nums.filter(|x| x % 2 == 0)
let evens_doubled = just_evens.map(|x| x * 2)
print(evens_doubled)
// expected: [4, 8, 12, 16, 20]
Agregação com reduce
reduce(fn, init) acumula os elementos em um único valor. O segundo argumento
é o valor inicial do acumulador.
Soma, produto, máximo manual e concatenação de strings via reduce.
// Feature: Array.reduce — aggregates elements into a single value
// When to use: summing, counting, finding max, building a final string.
use std::Array
let nums = [1, 2, 3, 4, 5]
// Sum — accumulator starts at 0.
let sum = nums.reduce(|acc: int, x: int| acc + x, 0)
print(sum) // expected: 15
// Product — accumulator starts at 1.
let prod = nums.reduce(|acc: int, x: int| acc * x, 1)
print(prod) // expected: 120
// Manual maximum.
let max = nums.reduce(|acc: int, x: int| if x > acc { x } else { acc }, 0)
print(max) // expected: 5
// Concatenate strings.
let words = ["hello", " ", "zolo", "!"]
let joined = words.reduce(|acc: str, w: str| acc + w, "")
print(joined)
// expected: hello zolo!
// Count even numbers in the list.
let inputs = [1, 2, 3, 4, 5, 6]
let evens_count = inputs.reduce(
|acc: int, x: int| if x % 2 == 0 { acc + 1 } else { acc },
0,
)
print(evens_count) // expected: 3
Busca com find e contains
contains testa pertença por valor; find retorna o primeiro elemento que
satisfaz o predicado, ou nil se nenhum casar.
contains simples; find com predicado; padrão if candidate != nil.
// Feature: Array.find / Array.contains — searching in arrays
// When to use: getting the first item matching a criterion, testing membership.
use std::Array
let nums = [1, 2, 3, 4, 5]
// contains — simple membership by value.
print(nums.contains(3)) // expected: true
print(nums.contains(99)) // expected: false
// find — first element satisfying the predicate, or nil.
let first_even = nums.find(|x| x % 2 == 0)
print(first_even) // expected: 2
let big = nums.find(|x| x > 100)
print(big) // expected: nil
// find on a string array.
let names = ["alice", "bob", "carol", "dave"]
let starts_c = names.find(|n| n.starts_with("c"))
print(starts_c) // expected: carol
// Combine with if-let for safe usage.
let candidate = nums.find(|x| x > 3)
if candidate != nil {
print("found: {candidate}")
} else {
print("nothing")
}
// expected: found: 4
Achatar e iterar com efeito colateral
flat() achata um nível de aninhamento. each executa um bloco por elemento
e retorna nil — adequado para side effects como print.
flat para unir sub-arrays; each para impressão; composição map + flat como flatMap.
// Feature: Array.flat / Array.each — flattening and iteration with side effects
// When to use: combining nested arrays, performing an action per item without a return.
use std::Array
// flat — flattens one level of nesting.
let nested = [[1, 2], [3, 4], [5]]
let flat = nested.flat()
print(flat)
// expected: [1, 2, 3, 4, 5]
// flat with arrays of varying sizes — all become items.
let mixed = [[1], [], [2, 3, 4], [5]]
print(mixed.flat())
// expected: [1, 2, 3, 4, 5]
// each — side effect per item, returns nil.
print("--- each ---")
Array::each([10, 20, 30], |x| print("item: {x}"))
// expected: --- each ---
// expected: item: 10
// expected: item: 20
// expected: item: 30
// Classic combination: flat + map for "flatMap".
let phrases = ["zolo lang", "rust roxo"]
let tokens = phrases.map(|s| s.split(" ")).flat()
print(tokens)
// expected: [zolo, lang, rust, roxo]
Desafio
Dado um array de palavras, use filter + map para obter apenas as palavras
com mais de 3 letras, em maiúsculas. Depois some os comprimentos com reduce.
Veja também