Introduction to Structs
Explore Rust structs to create custom data types that group related but different data. Understand how to declare, initialize, and access values within structs, including making instances mutable for updates. This lesson equips you with foundational skills for structured data management in Rust.
What Are Structs?
Structs consist of related items that potentially have different data types.
Structs are similar to tuples in this regard. However, unlike tuples, you must define the data type of the item within the struct.
Structs help to create custom data types.
Let’s consider a real life example. You know that a rectangle has two measurements, width and height. Suppose you have several rectangles which you can name. Once you have declared a rectangle items within the struct, you can initialize the values according to the type of rectangle. Suppose the dimensions of a rectangle may vary according to the color of the rectangle.
Declare a Struct
Structs are declared using a struct keyword followed by the name of the struct and then ...