Pular para o conteúdo

Bindings: let, let mut e var

Nesta página

Em Zolo, toda declaração de valor começa com let. Por padrão o binding é imutável — o compilador impede qualquer reatribuição posterior. Isso elimina uma classe inteira de bugs de aliasing sem custo em tempo de execução:

let cria um binding imutável; o tipo é inferido da expressão inicial.

01-let-immutable.zolo
Playground
// Feature: Immutable binding with `let`

// Syntax: `let name = expression`

// When to use: the default case — every value that does NOT need to

// be reassigned. Immutability by default avoids aliasing bugs.


// `let` creates an immutable binding; the type is inferred.

let name = "Alice"
let age = 30
let pi = 3.14159
let active = true

print(name)  // Alice

print(age)  // 30

print(pi)  // 3.14159

print(active)  // true


// Reassigning a `let` is a compile error (uncomment to test):

// name = "Bob"  // error: cannot assign to immutable binding


// Immutable bindings accept any expression as their initial value.

let total = 10 + 20 * 2
let greeting = "Hello, " + name + "!"
print(total)  // 50

print(greeting)  // Hello, Alice!

Quando o valor precisa mudar ao longo do tempo — um contador, um acumulador — adicione mut à declaração:

let mut permite reatribuição e operações de atribuição composta (+=, *=, -=).

02-let-mut.zolo
Playground
// Feature: Mutable binding with `let mut`

// Syntax: `let mut name = value` — then `name = new_value`

// When to use: counters, accumulators, any state that changes

// inside loops or branches. The `mut` keyword is required — Zolo

// treats mutability as opt-in to keep intent visible.


// Mutable: needs `mut` in the declaration.

let mut counter = 0
counter = counter + 1
counter = counter + 1
counter = counter + 1
print(counter)  // 3


// Classic accumulator in a loop.

let mut sum = 0
for i in 1..=10 {
  sum = sum + i
}
print(sum)  // 55


// Reassignment respects the type inferred at declaration.

let mut score: int = 100
score = 90
score = 80
print(score)  // 80


// Compound assignment — equivalent to `x = x + 5`.

let mut x = 10
x += 5
x *= 2
x -= 1
print(x)  // 29

var é um alias exato para let mut. Use var quando quiser economizar teclas ou quando planeja usar classes de armazenamento (var<lazy>, var<persistent> etc.) — a sintaxe <...> só existe em var:

var e let mut são intercambiáveis; somente var aceita modificadores de armazenamento.

11-var-alias.zolo
Playground
// Feature: `var` — alias for `let mut`

// Syntax: `var name = value` — then `name = new_value`

// When to use: anywhere `let mut` is valid. Same semantics, fewer

// keystrokes, and the gateway to storage classes (`var<...>`).

// `let mut` continues to work; both are accepted indefinitely.


// Mutable binding via the new shorter form.

var counter = 0
counter = counter + 1
counter = counter + 1
counter = counter + 1
print(counter)  // 3


// Equivalent in every way to `let mut counter = 0`.

let mut also_counter = 0
also_counter = also_counter + 1
print(also_counter)  // 1


// Compound assignment works the same.

var score = 100
score += 10
score -= 5
print(score)  // 105


// Type annotations also work.

var ratio: float = 0.5
ratio = ratio * 2
print(ratio)  // 1.0


// Why prefer `var`? Storage classes use the `var<...>` syntax —

// see 12-var-lazy.zolo, 13-var-persistent.zolo, etc. `let mut` does

// not accept storage classes (would be ambiguous to parse).

Desafio

Declare um acumulador var total = 0 e some os números de 1 a 100 com um for. Confirme que o resultado é 5050.

Buscar no Zolo

9 resultados

enespt-br