Structs help you group related data together in one place. They make your code organized and easy to understand.
When you want to represent a real-world object like a person or a car with multiple details.
When you need to keep several pieces of information together, like a book's title, author, and pages.
When you want to create your own custom data type to use in your program.
When you want to pass multiple related values around as one unit.
When you want to store data with different types in one variable.
Syntax
Rust
struct Name {
field1: Type1,
field2: Type2,
// more fields
}
The struct keyword starts the definition.
Each field has a name and a type, separated by a colon.
Examples
This defines a Person struct with a name and age.
Rust
struct Person {
name: String,
age: u8,
}
A Point struct to hold coordinates with decimal numbers.
Rust
struct Point {
x: f64,
y: f64,
}
A Book struct with title, number of pages, and availability status.
Rust
struct Book {
title: String,
pages: u32,
available: bool,
}
Sample Program
This program defines a Person struct, creates one named Alice who is 30 years old, and prints her details.
Rust
struct Person {
name: String,
age: u8,
}
fn main() {
let person = Person {
name: String::from("Alice"),
age: 30,
};
println!("Name: {}, Age: {}", person.name, person.age);
}
OutputSuccess
Important Notes
Use String::from to create a string for struct fields.
Fields must be separated by commas.
Struct names usually start with uppercase letters, and field names start with lowercase letters.
Summary
Structs group related data under one name.
Define structs with struct keyword and list fields with types.
Use structs to model real-world things in your code.
Practice
(1/5)
1. What is the purpose of defining a struct in Rust?
easy
A. To write a loop
B. To create a new function
C. To handle errors
D. To group related data together under one name
Solution
Step 1: Understand what a struct represents
A struct is used to group related pieces of data together, like a blueprint for an object.
Step 2: Compare options to the definition
Only To group related data together under one name correctly describes grouping related data under one name, which is the purpose of a struct.
Final Answer:
To group related data together under one name -> Option D
Quick Check:
Struct groups data = C [OK]
Hint: Structs group data fields under one name [OK]
Common Mistakes:
Thinking structs define functions
Confusing structs with loops
Assuming structs handle errors
2. Which of the following is the correct way to define a struct named Person with fields name (string) and age (u32)?
easy
A. struct Person { name: String, age: u32 }
B. struct Person(name: String, age: u32);
C. Person struct { name: String; age: u32 }
D. struct Person { name String, age u32 }
Solution
Step 1: Recall Rust struct syntax
Rust structs use the syntax: struct Name { field1: Type1, field2: Type2 } with commas and colons.
Step 2: Check each option
struct Person { name: String, age: u32 } matches correct syntax with colons and commas. Options A, B, and C have syntax errors like missing colons, wrong punctuation, or wrong order.
Final Answer:
struct Person { name: String, age: u32 } -> Option A
Quick Check:
Correct struct syntax uses colons and commas = D [OK]
Hint: Use colons and commas inside braces for struct fields [OK]
Common Mistakes:
Omitting colons between field names and types
Using semicolons instead of commas
Wrong order of keywords
3. What will be the output of this Rust code?
struct Point { x: i32, y: i32 }
fn main() {
let p = Point { x: 5, y: 10 };
println!("({}, {})", p.x, p.y);
}
medium
A. Point { x: 5, y: 10 }
B. (10, 5)
C. (5, 10)
D. Compilation error
Solution
Step 1: Understand struct initialization and field access
The struct Point is created with x=5 and y=10. Accessing p.x and p.y prints these values.
Step 2: Analyze the println! macro
The format string "({}, {})" prints the two values separated by a comma and space, so output is (5, 10).
Final Answer:
(5, 10) -> Option C
Quick Check:
Print fields x=5, y=10 as (5, 10) [OK]
Hint: Print struct fields by accessing them with dot notation [OK]
Common Mistakes:
Mixing up field values order
Expecting debug print without implementing Debug trait
Assuming struct prints automatically
4. Identify the error in this struct definition:
struct Car {
model String,
year: u32,
}
medium
A. Missing colon between field name and type for model
B. Missing semicolon after struct
C. Extra comma after last field
D. Incorrect struct keyword
Solution
Step 1: Check field syntax
Each field must have a colon between the name and type. Here, model String misses the colon.
Step 2: Verify other parts
Commas after fields are allowed, no semicolon needed after struct, and struct keyword is correct.
Final Answer:
Missing colon between field name and type for model -> Option A
Quick Check:
Field syntax requires colon = B [OK]
Hint: Field name and type must be separated by a colon [OK]
Common Mistakes:
Omitting colon between field and type
Thinking semicolon needed after struct
Removing commas after fields
5. You want to define a struct Book with fields title (String), author (String), and pages (u32). Then create an instance with title "Rust Guide", author "Alice", and pages 250. Which code correctly does this?
hard
A. struct Book(title: String, author: String, pages: u32);
let my_book = Book("Rust Guide", "Alice", 250);
B. struct Book { title: String, author: String, pages: u32 }
let my_book = Book { title: "Rust Guide".to_string(), author: "Alice".to_string(), pages: 250 };
C. struct Book { title String, author String, pages u32 }
let my_book = Book { title: "Rust Guide", author: "Alice", pages: 250 };
D. struct Book { title: String, author: String, pages: u32 }
let my_book = Book { title: "Rust Guide", author: "Alice", pages: "250" };
Solution
Step 1: Check struct definition syntax
struct Book { title: String, author: String, pages: u32 }
let my_book = Book { title: "Rust Guide".to_string(), author: "Alice".to_string(), pages: 250 }; correctly defines the struct with colons and commas. struct Book(title: String, author: String, pages: u32);
let my_book = Book("Rust Guide", "Alice", 250); uses tuple struct syntax incorrectly for this case. struct Book { title String, author String, pages u32 }
let my_book = Book { title: "Rust Guide", author: "Alice", pages: 250 }; misses colons. struct Book { title: String, author: String, pages: u32 }
let my_book = Book { title: "Rust Guide", author: "Alice", pages: "250" }; uses a string for pages which should be u32.
Step 2: Check instance creation
struct Book { title: String, author: String, pages: u32 }
let my_book = Book { title: "Rust Guide".to_string(), author: "Alice".to_string(), pages: 250 }; converts string literals to String with to_string(), which is required. struct Book(title: String, author: String, pages: u32);
let my_book = Book("Rust Guide", "Alice", 250); uses tuple struct syntax incorrectly. struct Book { title: String, author: String, pages: u32 }
let my_book = Book { title: "Rust Guide", author: "Alice", pages: "250" }; uses wrong type for pages.
Final Answer:
struct Book { title: String, author: String, pages: u32 }
let my_book = Book { title: "Rust Guide".to_string(), author: "Alice".to_string(), pages: 250 }; -> Option B
Quick Check:
Struct fields need colons and correct types = A [OK]
Hint: Use colons for fields and convert literals to String [OK]
Common Mistakes:
Using tuple struct syntax instead of named fields
Omitting colons in struct fields
Assigning wrong types to fields
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.