What is a Struct?
A Struct (short for structure) is a custom data type that lets you package together and name multiple related values that make up a meaningful group.
rust
Creating Instances
To use a struct, we create an instance of it by specifying values for each field.
rust
Other Types of Structs
Wait! Rust has three ways to define a struct:
- Classic Structs: (The one above) Named fields.
- Tuple Structs: Named types, but fields have no names (just types). Useful for "Newtype" patterns.
- Unit-Like Structs: No fields at all. Useful for traits on types with no data.
rust
Implementation Blocks (impl)
We can define Methods and Associated Functions for our structs using the
impl block.rust
[!TIP] You can have multipleimplblocks for the same struct! This is useful for separating different groups of functionality.
Summary
| Term | Syntax | Description |
|---|---|---|
| Struct | struct Name { ... } | Defining the blueprint |
| Instance | let x = Name { ... } | Creating the object |
| Field | x.field_name | Accessing data |
| impl | impl Name { ... } | Defining behavior |
| Self | &self | Reference to the current instance |
| Tuple Struct | struct S(i32); | Fields identified by index (0, 1) |
| Unit Struct | struct S; | Zero-sized type |
| Method | fn f(&self) | Logic that operates on instances |
| Assoc. Fn | fn f() | Logic that belongs to the type (e.g., constructors) |
| Ownership | Structs take ownership of their fields (unless they are references) | |
| Privacy | Fields are private by default! Use pub to make them public. | |
| Derive | #[derive(Debug)] | Auto-generate common functionality |