Structs and enums
Structs
A struct groups named values, its fields:
struct User {
name: String,
age: i32,
}
let ada = User { name: "Ada", age: 36 };
ada.name;
"Ada"
Every field must be given when you make a struct. When a variable has the same name as a field, you can write just the name:
struct Point { x: i32, y: i32 }
let x = 3;
let y = 4;
Point { x, y };
Point { x: 3, y: 4 }
A struct can't be changed, unless it's made with mut, like
mut Point { x: 0, y: 0 }. Mutability shows
how that works.
.. makes a new struct from another one, with some fields changed:
struct Config { width: i32, height: i32, fullscreen: Bool }
let default = Config { width: 800, height: 600, fullscreen: False };
let big = Config { fullscreen: True, ..default };
big;
Config { width: 800, height: 600, fullscreen: True }
Tuple structs and unit structs
A tuple struct has numbered fields instead of named ones. It's useful to give a type to a value, so that it can't be mixed up with others:
struct Meters(f64);
struct Seconds(f64);
let distance = Meters(100.0);
let time = Seconds(9.58);
distance.0 / time.0;
10.438413361169102
A unit struct has no fields at all: struct Marker;.
Enums
An enum is a type whose values are one of several variants:
enum Direction { North, East, South, West }
let heading = Direction.East;
heading;
East
Variants are named after their enum, as in Direction.East. Variants can
carry data, like tuple structs or structs do:
enum Shape {
Circle(f64),
Rect { w: f64, h: f64 },
Dot,
}
[Shape.Circle(1.0), Shape.Rect { w: 2.0, h: 3.0 }, Shape.Dot];
[Circle(1.0), Rect { w: 2.0, h: 3.0 }, Dot]
To look inside an enum value, you use pattern matching.
If you use the variants of an enum a lot, import Name.*; lets you write
them without the enum's name:
enum Direction { North, East, South, West }
import Direction.*;
fn turn_right(d: Direction) -> Direction = match d {
North => East,
East => South,
South => West,
West => North,
};
turn_right(West);
North
Functions of a type
A function can be put in the namespace of a struct or enum, by writing the
type's name and a . before the function's name. It's called the same way,
like a variant:
struct Point { x: i32, y: i32 }
fn Point.origin() -> Point = Point { x: 0, y: 0 };
fn Point.at(x: i32, y: i32) -> Point = Point { x, y };
let p = Point.at(3, 4);
(Point.origin().x, p.y);
(0, 4)
This is useful for functions that make a value of the type, so that
Point.origin and, say, Line.origin don't need different names. The name
is only in the type's namespace: origin on its own still means whatever
else has that name.
Only the file that defines a type can add functions to it. They're
private like other items, unless you write pub, and they come along when
you import the type.
Generic types
Structs and enums can have type parameters, between [ and ], so that
they work for any type of content:
struct Pair[A, B] { first: A, second: B }
let p = Pair { first: 1, second: "one" };
let q: Pair[Bool, f64] = Pair { first: True, second: 0.5 };
(p.second, q.first);
("one", True)
Pair[Bool, f64] is the type of a pair of a Bool and an f64.
Option
One generic enum is so useful that the prelude defines it for you:
enum Option[T] { None, Some(T) }
An Option[T] is either a value of type T (Some(value)), or nothing
(None). Bolt has no null. A function that may not have a result returns
an Option instead, so the possibility is part of its type: an
Option[i32] can't be used where an i32 is expected, and you have to look
inside it first:
fn find_even(xs: [i32]) -> Option[i32] {
for x in xs {
if x % 2 == 0 {
return Some(x);
}
}
None
}
(find_even([1, 3, 4, 5]), find_even([1, 3]));
(Some(4), None)
The prelude also has List[T], a linked list made of List.Cons(head, tail) and List.Nil, with the functions cons, head and tail.
Next: pattern matching.