Structs, enums and case
Group values with struct, list the possible forms of a value with enum, and take them apart with case.
So far each variable held one number or one piece of text. Real programs need
to keep several values together, and to say that a value is one of a few
possible kinds. A struct does the first. An enum does the second. case
is how you look at an enum.
Structs
A struct is a record with named fields. Declare it outside any verb. Each field has a name and a type:
Read the program from the top:
struct Point { x: Int, y: Int, }declares the type.Point { x: 3, y: 4, }creates a value. You write every field, with its name.point.xreads a field, andpoint.x = 10;changes it. You can change fields becausepointis anergvariable.sum(point: abs point)hands the whole struct to a verb that only reads it. The verb readspoint.xandpoint.ythe same way.
The compiler checks that you fill in every field and that you only use fields that exist:
A struct is not a number, so it is not copied when you assign it to a new
variable: it moves. This is true even when all its fields are plain numbers.
After erg other = point; the name point is gone.
Copying a struct
If you need two separate points, the struct must say that it may be copied.
Write meta copy above its declaration, and write each copy yourself with
copy. No copy ever happens without a copy in the source:
other is a new Point, and changing point does not change it. Without
meta copy, the same copy is an error:
meta copy is only accepted when every field can be copied: a number, a
Bool, a float, or another meta copy type. A struct with a String field
cannot be meta copy. Enums work the same way.
Enums
An enum is a value that is exactly one of a fixed list of variants. A traffic light is red, yellow or green, and never two of them at once:
You write a variant as the enum name, a dot and the variant name:
Light.Yellow. case looks at the value and runs the arm that matches. Here
case is used as a value, like if in
Conditions and loops. Each arm is
pattern => result, and the arms are separated by commas.
Variants that carry data
A variant can carry values. There are three kinds in one enum below:
Dotcarries nothing.Square(Int)carries oneInt.Rect {width: Int, height: Int}carries named fields.
You build a variant with data like a call: Shape.Square(4), or with the
field names, Shape.Rect(width: 3, height: 5). In case, a pattern gives
names to the data it holds. Shape.Square(side) names the number side for
that arm. Shape.Rect {width: w, height: h} names the two fields w and h.
case must cover every possibility
case has to handle every variant. If you forget one, the program does not
compile, and the message names the missing variant:
This is the main reason to use an enum. When you add a new variant later, the
compiler shows you every case that needs a new arm.
The wildcard
To cover all the remaining possibilities at once, use _. It matches
anything. This also lets you use case on numbers:
Arms are tried from the top. An arm after _ could never run, so the compiler rejects it. Put _ last.
case on a Bool
A Bool has two values, so true and false cover everything and you do
not need _:
If an arm is missing, the message names the value:
A _ after both values can never match, so it is an error. The same is true
for a _ after arms that already name every variant of an enum:
Numbers have too many values to list, so a case on a number always needs
_, as in the example before.
Guards
An arm can have a condition. Write if after the pattern. The arm is used
only when the pattern matches and the condition is true:
Reading.Value(v) if v > 100 catches large readings. A reading of 5 does not
pass the guard and falls to the next arm, Reading.Value(_). The _ inside
the parentheses means "there is a value, and I do not need it".
case as a statement
case does not need to produce a value. Used as a statement, each arm does
something and the whole case ends with a semicolon. The same program also
shows that two values of a plain enum, one without data, can be compared with
== and !=:
Enums that carry data cannot be compared with == as a whole. Take them
apart with case instead.
What you learned
struct Name { field: Type, }groups named values. Create one with every field, read and change fields with a dot.enum Name { A, B(Int), C {x: Int}, }lists the possible forms of a value.casematches a value against patterns, binds the data inside a variant, and must cover every possibility._matches the rest, andifadds a condition.- Assigning a struct to a new variable moves it. A struct declared with
meta copycan be copied, and the copy is written ascopy(value: abs x). - A
caseon aBoolis complete withtrueandfalse. A_after every value is an error.
Next: Ownership roles.