Structs, Enums, and Pattern Matching in Rust
Share
Moving Beyond Separate Variables
Imagine a program that needs to represent a course lesson.
A lesson might have a title, duration estimate, topic, and completion state. These could all be stored as separate variables, but doing so becomes difficult when many lessons are involved.
A struct allows those related values to be grouped together.
Instead of thinking about four unrelated variables, the program can work with one meaningful entity: a lesson.
This improves organization because the structure communicates what the fields belong to.
The same idea can apply to many situations:
- user settings
- course information
- configuration records
- coordinates
- messages
- tasks
- measurements
- application states
The specific subject changes, but the principle remains the same: related information can be represented as one structured type.
Defining Structs
A struct defines the shape of a value.
It describes which fields exist and what types they use. Individual values created from that definition can then contain different data while keeping the same structure.
This makes it easier to reason about related information.
For example, if several records all use the same fields, a struct makes those expectations explicit. Anyone reading the code can see what information belongs to that type.
Structs can also work with methods.
Methods allow behavior to be placed near the data it relates to. Instead of scattering operations across unrelated functions, certain actions can be organized around the type itself.
This can make code easier to navigate when the project contains many related operations.
When Enums Become Useful
Structs work well when a value has a consistent set of fields. Enums are useful when a value can represent one of several distinct possibilities.
Consider a learning resource that may be in one of several states:
- planned
- active
- completed
- archived
Instead of representing these states with loosely related strings or numbers, an enum can describe the available variants directly.
This gives the program a defined set of possibilities.
Enums become even more interesting because individual variants can carry additional data.
One variant might contain no extra information, while another might include a date, message, or structured value.
This allows a single type to represent related cases without forcing every case into the same shape.
Modeling Different States
Enums are especially useful when a program needs to make decisions based on state.
A value might represent:
- an operation that worked or did not work
- a value that exists or is absent
- several different message types
- multiple user actions
- different data categories
The important idea is that each possibility is explicitly represented.
This can make program logic easier to follow because the possible states are described by the type itself.
Pattern Matching
Once a program uses enums, it needs a clear way to respond to their variants.
Pattern matching provides that structure.
A match expression can inspect a value and define what should happen for each relevant pattern.
This is more structured than writing many unrelated condition checks because the branches are directly connected to the possible shapes of the value.
A common mental model is:
Value → Identify Pattern → Run Matching Logic
Pattern matching can also extract information stored inside a variant.
For example, if one enum variant contains additional data, the matching branch can make that data available for use.
This creates a close connection between data representation and program behavior.
Structs and Enums Together
Structs and enums are often used together rather than separately.
A struct may contain an enum field representing its current state. An enum variant may contain a struct representing detailed information.
This combination makes it possible to model more complex situations while keeping the structure readable.
For example, a learning record could contain general information in a struct and use an enum to represent its current stage.
Another program might use an enum to represent several event types, with each variant containing a different struct.
These combinations allow programmers to describe the shape of their data directly in code.
Methods and Implementation Blocks
Rust allows methods to be associated with types through implementation blocks.
This helps place related behavior near the structure it works with.
A method may read fields, change data when appropriate, calculate a result, or provide another way to construct a value.
The key idea is organization.
Instead of treating data and behavior as completely separate topics, Rust allows them to be connected in a deliberate way.
A Useful Learning Sequence
Learners studying these concepts can approach them gradually:
Fields → Structs → Methods → Enums → Variants → Pattern Matching
Begin with a small struct containing two or three fields. Add a method. Then introduce an enum with a few simple variants. Finally, use pattern matching to respond to each variant.
Small examples make it easier to see how the pieces connect.
As these concepts become familiar, they can be combined into larger models representing real program state and behavior.
Structs, enums, and pattern matching are valuable because they help programmers move from isolated values toward organized models. They provide a clear vocabulary for representing information and describing how a program should respond to different forms of that information.