Structs

Learn to create your first struct, a fundamental building block for defining custom data types, and how to modify its fields.

In Rust, a statically typed systems programming language, structs play a fundamental role in organizing and representing data. A struct, short for “structure,” allows you to create custom data types by grouping together variables of different types under a single name. This facilitates the organization and manipulation of related data in a concise and efficient manner. Unlike some other languages, Rust’s structs provide a powerful combination of flexibility, safety, and performance. Structs are different from enums in such a way that they allow us to bundle different datatypes under a single name, whereas, enums are used to define a type that can have different variants, each with its own data (if any).

Creating structs

To begin, we’ll delve into the syntax of struct declaration, discuss how to instantiate and initialize struct instances and explore methods to access and modify their fields. Let’s start by creating a User struct. This will contain all the basic information, such as the user’s username, email and whether they’re active or not.

Get hands-on with 1400+ tech skills courses.