Structs & Enums

A struct is a simple Rust data structure that can be defined and customised by the programmer.

Structs are made up of fields, internal pieces of data that also need their own data types:


    

Like with variables, each struct, and each field, also needs its own alias.

Struct fields can also be other structs:


    

Exercise:
Try adding a field of type char with the alias "character" to the struct defined below, before printing it:


    

Enums, short for enumerations, are tiny pieces of data that, similar to structs, are defined entirely by the programmer as a custom data structure, and can only be one of multiple variants (sometimes called discriminants):


    

Enums can have their own variants defined inline as custom data types (like with the above example). Variants with an alias but with no value are said to be of the unit type.

In a similar way to structs, enums can have multiple fields of different types and data structures:


    

Exercise:
Try adding a tuple variant of length 6 with the alias "Types", and six different data types within, to the enum defined below: