As developers, we create programs and applications to solve problems — problems that we encounter on a daily basis that usually stem from everyday life. More often than not, these problems are complex.
Wouldn’t it be nice if we had some sort of data type that would help us model this complexity of everyday life? That way we wouldn’t have to solely rely on basic data types like
Ints, or some collection of these types like arrays, sets, or dictionaries; this data type would combine all these facets and allow us to closely mimic real-world objects.
We don’t have to wonder about this hypothetical situation! That’s what the structure data type, also known as a struct, is for!
With structures, we can create blueprints of everyday objects and use this blueprint to make specific instances of these objects directly in our program.
Let’s imagine that we are building an app and we need to store some user information such as:
- Their bio
We could make a user structure that serves as the blueprint when creating individual users. Each individual user is an instance of the blueprint but they would still contain their own unique values.
This idea of a blueprint structure provides customization and programmatically allows us to create as many different users as we want. Essentially, we were able to take a complex object like a user and represent them in code!
In this lesson, we’ll learn how to create these structures and how to customize them to suit them to our needs. 🏗️
Look over the provided GIF.
The blueprint (Structure) shows the possible appearance of a constructed 2-floor house with a triangular roof.
The first instance follows the blueprint exactly.
The second instance removes one level and only has a single floor.
The third instance adds one level and has three floors.