Docs/Language/Structs & Enums
Structs & Enums
Product types, algebraic sum types, impl blocks, and exhaustive pattern matching.
~3 min read
On this page
Structs and impl blocks
struct Point {
x: f64,
y: f64,
}
impl Point {
// Associated function (constructor)
fn new(x: f64, y: f64) -> Point {
return Point { x: x, y: y };
}
// Method: takes self by value
fn dist2(self, other: Point) -> f64 {
val dx = self.x - other.x;
val dy = self.y - other.y;
return dx * dx + dy * dy;
}
// Mutating method
fn translate(self: &mut Point, dx: f64, dy: f64) {
self.x += dx;
self.y += dy;
}
}
fn main() -> i64 {
var a = Point::new(0.0, 0.0);
val b = Point::new(3.0, 4.0);
println(a.dist2(b)); // 25
a.translate(1.0, 2.0);
println(a.y); // 2
println(a == Point { x: 1.0, y: 2.0 }); // true: == compares fields
return 0;
}The receiver is self (a copy) or self: &mut T (changes the caller’s value); &self is not valid syntax. Every field is named in a struct literal: Point { x: x, y: y } (there is no Point { x, y } shorthand, and no tuple or unit structs). ..base copies the remaining fields from another value.
Visibility
Items and fields are private to their module unless marked pub:
// shapes.fd
pub struct Config {
pub name: str,
internal_hash: u64, // private to this module
}
pub fn make_config() -> Config {
return Config { name: "app", internal_hash: 7 };
}Reading make_config().internal_hash from another module is a compile error:
Privacy error: field 'internal_hash' of struct 'Config' is private. It was defined in module 'shapes', which is not the current module 'main'.Enums
Variants are unit (Quit) or carry values (Move(i64, i64)); there are no struct-like variants and no explicit discriminants. Enums take impl blocks too:
enum Message {
Quit,
Write(str),
Move(i64, i64),
}
impl Message {
fn process(self) {
match self {
Message::Quit => { println("Quitting"); }
Message::Write(text) => { println(text); }
Message::Move(x, y) => { println(f"Move to ({x}, {y})"); }
}
}
}
fn main() -> i64 {
Message::Write("hi").process(); // hi
val m = Message::Move(3, 4);
m.process(); // Move to (3, 4)
return 0;
}Pattern matching
Patterns can be variants with bindings, non-negative integer and string literals, ranges, guards (s if s > 90), a name (binds anything) and _. There are no nested, tuple, struct or | patterns. In a match used as a value an arm can be a single expression; in a match statement every arm is a block: 1 => { ... }.
fn size(x: i64) -> str {
return match x {
0..=9 => "single digit",
10..=99 => "double digit",
_ => "large",
};
}
fn command(key: str) -> str {
return match key {
"q" => "quit",
"h" => "help",
_ => "unknown",
};
}
fn main() -> i64 {
println(size(42)); // double digit
println(command("h")); // help
val opt: Option<i64> = Option::Some(4);
val doubled = match opt {
Some(v) => v * 2, // the enum name can be left out here
Option::None => 0, // but not here: a bare `None` is a new variable
};
println(doubled); // 8
return 0;
}A match on an enum must handle every variant:
enum Color { Red, Green, Blue }
fn main() -> i64 {
val c = Color::Red;
val n = match c {
Color::Red => 1,
Color::Green => 2,
};
return n;
}Type error: non-exhaustive match at line 5 in main — missing variant(s): Color::Blue