Cuándo Usar Cada Tipo
En esta página
Antes de modelar un dominio, vale la pena revisar qué tipo resuelve cada problema de forma más directa. El ejemplo a continuación demuestra todos los tipos en situaciones reales:
Cheatsheet: array, set, map, tupla, struct, enum, newtype y enum con datos.
12-when-to-use.zolo
// Feature: Cheatsheet — when to use which collection/type
// Syntax: various — this file demonstrates choosing the right
// structure for the right problem.
// When to use: review concepts before modeling a domain.
use std::String
use std::Map
use std::Set
// 1) ARRAY — when order matters and there are legitimate duplicates.
let log_lines = ["start", "tick", "tick", "stop"]
print(log_lines.len()) // 4
// 2) SET — when you only want to know "is it in or not".
let visited = Set::new()
visited.add("/home")
visited.add("/about")
visited.add("/home") // duplicate ignored
print(visited.len()) // 2
// 3) MAP — key lookup, counting, configuration.
let prices = #{banana: 3, apple: 5, orange: 4}
print(prices["banana"]) // 3
// 4) TUPLE (= array with destructure) — lightweight grouping, multi-return.
fn split_name(full: str) -> [str] {
let parts = full.split(" ")
return [parts[0], parts[1]]
}
let (first, last) = split_name("Alice Silva")
print("{first} / {last}")
// 5) STRUCT — when fields have NAMES and the domain is stable.
struct User {
name: str,
age: int,
}
let u = User { name: "Bob", age: 25 }
print("{u.name} ({u.age})")
// 6) ENUM — when the value is "one of these mutually exclusive shapes".
enum HttpMethod {
Get,
Post,
Put,
Delete,
}
fn safe(m: HttpMethod) -> bool {
return match m {
HttpMethod::Get => true,
HttpMethod::Post => false,
HttpMethod::Put => false,
HttpMethod::Delete => false,
}
}
print(safe(HttpMethod::Get)) // true
print(safe(HttpMethod::Post)) // false
// 7) NEWTYPE — when two types have the SAME physical shape but
// must be distinct in the domain (IDs, units).
newtype CustomerId(int)
newtype InvoiceId(int)
let cid = CustomerId.new(10)
let iid = InvoiceId.new(20)
fn use_ids(c: CustomerId, i: InvoiceId) {
print("c={c.unwrap()} i={i.unwrap()}")
}
use_ids(cid, iid)
// 8) ENUM with data — sum types (ADT). Ideal for AST,
// messages, parsing.
enum Json {
Num(float),
Str(str),
Bool(bool),
Null,
}
fn show(j: Json) -> str {
return match j {
Json::Num(n) => "num({n})",
Json::Str(s) => "str({s})",
Json::Bool(b) => "bool({b})",
Json::Null => "null",
}
}
print(show(Json::Num(3.14)))
print(show(Json::Str("hi")))
print(show(Json::Bool(true)))
print(show(Json::Null))
Resumen de la regla de oro:
| Situación | Tipo |
|---|---|
| Lista ordenada, duplicados permitidos | [T] (array) |
| Pertenencia, sin duplicados | Set |
| Clave → valor, búsqueda por clave | Map |
| 2–3 valores sin nombre, múltiples retornos | tupla (let (a, b) = ...) |
| Campos con nombre, dominio estable | struct |
| El valor es "uno de N formatos" | enum |
| Dos tipos con el mismo formato físico, distintos en el dominio | newtype |
Consulta también