1. What is a Struct?
A struct (structure) is a custom data type that allows us to group multiple related values under one name.
Each value inside a struct is called a field.
Example
Output:
Understanding the Code
structis the keyword used to define a structure.Studentis the struct name.name,age, andmarksare fields.String,u32, andf64are field data types.Student { ... }creates an instance of the struct.student.nameaccesses the name field.
2. Struct Syntax
Creating an instance:
Remember: When creating an instance, you must provide all required fields unless you use struct update syntax.
3. Accessing Struct Fields
We use dot notation (.) to access individual fields.
Example
Output:
4. Mutable Structs
By default, struct instances are immutable.
To modify their fields, declare the entire instance using mut.
Example
Output:
Important: Rust does not allow you to declare only one struct field as mutable. Mutability is applied to the entire instance.
5. Creating Multiple Struct Instances
We can create multiple instances of the same struct.
Example
Output:
Both instances use the same structure but contain different values.
6. Struct Update Syntax
Sometimes we want to create a new struct instance using values from an existing instance.
Rust provides struct update syntax using ...
Example
Output:
Understanding ..student1
It means:
- Use the explicitly provided values first.
- Take all remaining fields from
student1.
Important: Struct update syntax moves or copies remaining field values according to their types. Because name is a String, it is moved from student1 into student2 in this example. Therefore, student1 can no longer be used as a complete struct.
7. Tuple Structs
A tuple struct is similar to a tuple, but it has a custom type name.
Unlike a regular struct, tuple structs do not use named fields.
Example
Output:
Here:
Coloris the tuple struct name.redis an instance..0,.1, and.2access the fields by position.
Another Example
Output:
8. Unit-Like Structs
A unit-like struct is a struct without any fields.
Syntax
Example
Output:
Unit-like structs are useful when we need a custom type without storing any data.
9. Structs with Functions
We can create functions that accept struct instances as parameters.
Example
Output:
Here:
&Studentmeans the function borrows a student instance.- The function can read its fields.
- Ownership remains with
main().
10. Structs with Mutable References
We can pass a mutable reference to a function when we want that function to modify a struct.
Example
Output:
Here:
&mut Studentaccepts a mutable reference.student.marks += 5modifies the original struct.- Ownership is not transferred.
11. Deriving Debug for Structs
Normally, we cannot print a struct directly using {} or {:?} unless the appropriate formatting trait is implemented.
Rust provides #[derive(Debug)] to automatically implement the Debug trait for many simple structs.
Example
Output:
Pretty Debug Formatting
Use {:#?} for a more readable, multi-line output.
Output:
12. Difference Between Struct and Tuple
Feature | Struct | Tuple |
|---|---|---|
Fields | Named | Positional |
Access | Dot notation with field name | Dot notation with index |
Readability | More descriptive | Less descriptive for many values |
Data types | Different types allowed | Different types allowed |
Example |
|
|
13. Quick Reference
Concept | Syntax |
|---|---|
Define struct |
|
Create instance |
|
Access field |
|
Mutable instance |
|
Update field |
|
Struct update |
|
Tuple struct |
|
Unit struct |
|
Debug output |
|
Borrow struct |
|
Mutable borrow |
|