Hint: Remember: fields use colon and commas inside braces [OK]
Common Mistakes:
Using '=' instead of braces
Missing colons between field names and types
Using semicolons instead of commas
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. Compilation error
B. (5, 10)
C. Point { x: 5, y: 10 }
D. (10, 5)
Solution
Step 1: Understand struct field access
The struct Point has fields x and y. The code creates p with x=5 and y=10.
Step 2: Analyze the print statement
The println! macro prints p.x and p.y in order, so output is (5, 10).
Final Answer:
(5, 10) -> Option B
Quick Check:
Print fields in order = (5, 10) [OK]
Hint: Print fields in order: x then y [OK]
Common Mistakes:
Swapping x and y values
Expecting debug print of whole struct
Thinking code won't compile
4. Identify the error in this Rust struct usage:
struct Car {
model: String,
year: u32
}
fn main() {
let my_car = Car { model: "Tesla", year: "2020" };
}
medium
A. Struct fields must be mutable
B. Missing semicolon after struct definition
C. Model field should be an integer
D. Year field has wrong type: string instead of u32
Solution
Step 1: Check struct field types
The Car struct defines year as u32 (a number).
Step 2: Check struct initialization
The code assigns year: "2020", which is a string, not a number. This causes a type mismatch error.
Final Answer:
Year field has wrong type: string instead of u32 -> Option D
Quick Check:
Field types must match exactly [OK]
Hint: Match field types exactly when creating structs [OK]
Common Mistakes:
Using quotes around numbers
Thinking fields must be mutable
Missing semicolon is not the main error here
5. You want to model a Book with title, author, and number of pages using a Rust struct. Which approach best uses structs to organize this data?
hard
A. Use a struct with fields: title (String), author (String), pages (u32)
B. Use separate variables for title, author, and pages without a struct
C. Use a tuple with (String, String, u32) without naming fields
D. Use a struct with only one field holding all data as a single string
Solution
Step 1: Understand the goal of modeling data
We want to group related properties of a book clearly and access them by name.
Step 2: Evaluate options for clarity and organization
Use a struct with fields: title (String), author (String), pages (u32) uses a struct with named fields for each property, making code clear and organized. Use separate variables for title, author, and pages without a struct scatters data, Use a tuple with (String, String, u32) without naming fields uses unnamed tuple fields, and Use a struct with only one field holding all data as a single string mixes all data into one string, losing structure.
Final Answer:
Use a struct with fields: title (String), author (String), pages (u32) -> Option A
Quick Check:
Structs model real-world things with named properties [OK]
Hint: Use structs to name and group related data clearly [OK]
Common Mistakes:
Using tuples without field names
Storing all data in one string
Avoiding structs and using separate variables
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.