Structs
Rust is a systems programming language focused on speed, memory safety, and fearless concurrency — and structs are how you model real-world things by grouping related data together.
Learn Structs in our free Rust course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Rust course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll define structs with named fields, create instances, and attach behaviour with methods in impl blocks.
What You'll Learn in This Lesson
1️⃣ Defining and Using Structs
A struct declares a custom type with named, typed fields. You create an instance by setting every field, then read fields with dot notation. To change a field, the whole instance must be declared mut — Rust does not let you mark just one field mutable.
Each instance owns its data. Mutability applies to the whole instance, so user2 had to be mut before we could flip its active field.
2️⃣ Methods and Associated Functions
An impl block attaches behaviour to a struct. A method takes &self and is called with dot syntax on an instance. An associated function has no self and is called on the type with :: — perfect for constructors like new .
The {'Rectangle '} uses field init shorthand : when a variable has the same name as a field, you can write the field once. And let us print the whole struct with {' '} .
Your turn. Fill in the blanks marked ___ , then run it.
Define a struct with a constructor and two methods. Run it with cargo run and check the output.
📋 Quick Reference — Structs
Practice quiz
What does a struct group together?
- Unrelated functions
- Only numbers
- Related data under named fields
- Threads
Answer: Related data under named fields. A struct groups related data, each value in a named, typed field.
How do you read a field of a struct instance?
- With dot notation, e.g. user.name
- With ::
Answer: With dot notation, e.g. user.name. Fields are accessed with dot notation like user.name.
To change a single field, what must be true of the instance?
- The field must be pub
- It must be cloned
- It must be boxed
- The whole instance must be mut
Answer: The whole instance must be mut. Rust requires the whole instance to be mut; you cannot mark one field mutable.
What is the difference between a method and an associated function?
- Methods are faster
- A method takes self; an associated function does not
- Associated functions take self
- There is none
Answer: A method takes self; an associated function does not. Methods take self and use dot syntax; associated functions take no self and use ::.
How is an associated function like new typically called?
- Rectangle::new(4, 3)
- rect.new()
- new(Rectangle)
- Rectangle.new
Answer: Rectangle::new(4, 3). Associated functions are called on the type with ::, e.g. Rectangle::new(4, 3).
Why do methods usually take &self instead of self?
- It is shorter
- self is invalid
- &self borrows so the instance stays usable after the call
- It avoids the heap
Answer: &self borrows so the instance stays usable after the call. &self borrows the instance for reading without consuming it.
What does field init shorthand allow?
- Skipping fields
- Writing a field once when a variable shares its name
- Default values
- Renaming fields
Answer: Writing a field once when a variable shares its name. When a variable has the same name as a field, you write the field once.
What does #[derive(Debug)] enable?
- Cloning
- Equality
- Mutation
- Printing with {:?}
Answer: Printing with {:?}. Deriving Debug lets you print the struct with {:?} or {:#?}.
For impl Rectangle with area, what does Rectangle::new(4,3).area() return?
- 7
- 12
- 1
- false
Answer: 12. area multiplies width by height: 4 * 3 = 12.
What happens if you create a struct but omit a field?
- It defaults to 0
- It is set to None
- A compile error (missing field)
- Nothing
Answer: A compile error (missing field). Every field must be provided (unless using struct update syntax).