Structs: Derive and Validation
@derive generates standard trait implementations for a struct without manual
code. The available traits are Eq (comparison via ==), Clone
(the .clone() method), and Debug (structured printing via print). Multiple
traits can be combined on a single line:
Eq on Color, Clone on Config, and all three traits together on Vec3.
// Feature: `@derive(Trait, ...)` — generates common implementations
// Syntax: `@derive(Eq)`, `@derive(Clone)`, `@derive(Debug)`, or multiple.
// When to use: structs where you want to compare (`==`), copy (`.clone()`),
// or print (`tostring`) without writing boilerplate.
@derive(Eq)
struct Color {
r: int,
g: int,
b: int,
}
let red = Color { r: 255, g: 0, b: 0 }
let also_red = Color { r: 255, g: 0, b: 0 }
let blue = Color { r: 0, g: 0, b: 255 }
print("red == also_red? {red == also_red}")
// expected: red == also_red? true
print("red == blue? {red == blue}")
// expected: red == blue? false
@derive(Clone)
struct Config {
host: str,
port: int,
}
let cfg = Config { host: "localhost", port: 8080 }
let backup = cfg.clone()
print("backup = {backup.host}:{backup.port}")
// expected: backup = localhost:8080
// Multi-derive — several capabilities in a single line.
@derive(Debug, Clone, Eq)
struct Vec3 {
x: float,
y: float,
z: float,
}
let v = Vec3 { x: 1.0, y: 2.0, z: 3.0 }
let w = v.clone()
print(v)
// expected: Vec3 { x: 1.0, y: 2.0, z: 3.0 }
print("v == w? {v == w}")
// expected: v == w? true
@validate generates the .validate() method on the struct. It checks all
fields and returns true if none is nil — sufficient for validating form
payloads and API request bodies before processing:
Simple struct validation and composition with @serialize.
// Feature: `@validate` — generates `.validate()` that checks for non-nil fields
// Syntax: `@validate` on a struct. The method returns `true` if all
// fields are non-nil.
// When to use: simple payload validation (form, API body) before
// processing.
@validate
struct Registration {
username: str,
email: str,
age: int,
}
let valid = Registration { username: "alice", email: "[email protected]", age: 25 }
print("valid? {valid.validate()}")
// expected: valid? true
// `@validate` also pairs well with @serialize/@deserialize.
@validate
@serialize
struct LoginRequest {
user: str,
password: str,
}
let req = LoginRequest { user: "bob", password: "secret" }
print("login valid? {req.validate()}")
// expected: login valid? true
print("login json = {req.to_json()}")
// expected: login json = {"user":"bob","password":"secret"}
Challenge
Create a struct Address with fields street, city, and zip, annotate it
with @derive(Debug, Eq) and @validate. Instantiate two equal objects and
confirm that == returns true and .validate() returns true.
See also