CHAPTER 12
Beginner
Structs in Rust
Updated: May 18, 2026
5 min read
# CHAPTER 12
Structs in Rust
1. Chapter Introduction
If you are building an Employee Management system, you cannot pass around 5 separate variables (firstname, lastname, email, age, active) every time you want to process an employee. You need to group related data together.
In Object-Oriented languages like Java or C++, you use a class. Rust does not have classes. Instead, Rust uses Structs to organize data, and separates the logic (methods) into a distinct block.
2. Learning Objectives
By the end of this chapter, you will be able to:- Define and instantiate a standard Struct.
- Access and modify struct fields.
-
Implement methods using the
implblock.
-
Understand the
&selfparameter in methods.
- Create Associated Functions (similar to static methods/constructors).
3. Defining and Instantiating a Struct
A struct allows you to name and package multiple related values.1. Define the Struct
rust
2. Instantiate the Struct
rust
4. Struct Update Syntax
If you want to create a new user based on an existing user but only change the email, you can use the.. syntax.
rust
5. Adding Methods using the impl Block
Structs hold data, but they don't hold logic. To attach actions to a struct, we use an Implementation block (impl).
Inside the impl block, methods must take a reference to self as their first parameter. &self is short for self: &Self, meaning "borrow the struct instance".
rust
6. Associated Functions (Constructors)
You can write functions inside animpl block that do NOT take &self as a parameter. These are called Associated Functions. They are associated with the Struct type itself, not a specific instance. They are commonly used as constructors (like String::new()).
rust
7. Tuple Structs
If you want to group data but don't want to bother naming the fields, you can use a Tuple Struct. These are great for giving specific semantic meaning to basic tuples.
rust
8. Common Mistakes
-
Partial Mutability: Trying to make one field of a struct mutable while leaving others immutable. Rust does not allow this. If you declare
let mut user = User{...}, the *entire* struct is mutable.
-
Forgetting
&onself: If you writefn area(self), you are taking *ownership* of the struct! Calling.area()will instantly destroy the struct after it returns the area. Almost always use&self(borrowing) for methods.
9. Best Practices
-
Use
Stringover&strin Structs (for now): If you try to store a reference&strinside a struct, the compiler will require you to use "Lifetimes" (which we will learn in Chapter 18). For beginners, always use ownedStringtypes inside structs.
10. Exercises
-
1.
Define a
Carstruct withmake(String),model(String), andyear(u32).
-
2.
Write an
implblock with an associated functionnew(make, model, year)that returns a newCar.
-
3.
Write a method
print_details(&self)that prints the car's info.
11. MCQs with Answers
Q1. Does Rust have the class keyword?
a) Yes b) No, it uses struct to group data
Answer: b) No, it uses struct.
Question 2
How do you access a field inside an instantiated struct?
Question 3
If you want to modify a struct's fields, what must you do?
Question 4
What keyword is used to attach methods to a struct?
Question 5
What must be the very first parameter of a standard struct method?
Question 6
What happens if a method uses self instead of &self?
impl block that does NOT take &self as a parameter, often used as a constructor b) A function linked to the internet
Answer: a) A function that does NOT take &self.
Question 8
How do you call an Associated Function?
struct Color(i32, i32, i32);)
Answer: b) A named struct where the fields have no names.
Question 10
Why is it difficult to put a &str (reference) inside a struct as a beginner?
12. Interview Questions
- Q: How does Rust separate data representation from behavior (logic) compared to traditional Object-Oriented languages?
-
Q: Differentiate between a Method and an Associated Function inside an
implblock.
13. Summary
Structs form the core of Rust's data modeling capability. By defining rigid data layouts and attaching specific, safe behaviors throughimpl blocks and the &self reference, Rust developers can build highly complex systems that are well-organized and entirely memory safe.