A struct in Rust is a custom data type that lets you group related values together under one name. Think of it like a box holding different items labeled with names.
Click to reveal answer
beginner
How do you access a field of a struct in Rust?
You use the dot (.) operator after the struct variable name, followed by the field name. For example, person.name accesses the name field of person.
Click to reveal answer
intermediate
What happens if you try to access a field that does not exist in a Rust struct?
Rust will give a compile-time error saying the field does not exist. This helps catch mistakes early before running the program.
Click to reveal answer
intermediate
Can you access fields of a struct through a reference in Rust?
Yes! Rust lets you use the dot operator on references to structs, and it automatically dereferences them to access the fields.
Click to reveal answer
beginner
Example: Given <code>struct Point { x: i32, y: i32 }</code> and <code>let p = Point { x: 5, y: 10 };</code>, how do you get the value of <code>x</code>?
You write p.x. This accesses the x field of the p variable, which will be 5.
Click to reveal answer
How do you access the field age of a struct variable person in Rust?
Aperson.age
Bperson->age
Cperson["age"]
Dperson::age
✗ Incorrect
In Rust, you use the dot operator to access fields of a struct, so person.age is correct.
What error will Rust give if you try to access a non-existent field height on a struct person?
AWarning: field might be missing
BRuntime error: field not found
CNo error, returns null
DCompile-time error: no field `height` on type `Person`
✗ Incorrect
Rust checks field names at compile time and will give an error if the field does not exist.
Can you access a struct field through a reference in Rust without manually dereferencing?
AYes, Rust auto-dereferences references when accessing fields
BNo, you must always dereference manually
COnly if the field is public
DOnly for primitive types
✗ Incorrect
Rust automatically dereferences references when you use the dot operator to access fields.
Given struct Car { model: String } and let c = Car { model: String::from("Sedan") };, what does c.model return?
AAn integer
BThe string "Sedan"
CA boolean
DA compile error
✗ Incorrect
c.model accesses the model field which holds the string "Sedan".
Which operator is used to access fields of a struct in Rust?
A[] (brackets)
B-> (arrow)
C. (dot)
D:: (double colon)
✗ Incorrect
The dot operator is used to access fields of structs in Rust.
Explain how to access fields of a struct in Rust and what happens if you try to access a field that does not exist.
Think about how you get a value from a labeled box and what Rust does if the label is missing.
You got /4 concepts.
Describe how Rust handles accessing struct fields through references.
Consider how Rust helps you avoid extra steps when using references.
You got /4 concepts.
Practice
(1/5)
1. In Rust, how do you access the field age of a struct instance named person?
easy
A. person::age
B. person->age
C. person["age"]
D. person.age
Solution
Step 1: Understand struct field access syntax
In Rust, fields of a struct instance are accessed using the dot (.) operator.
Step 2: Apply the dot operator to the instance and field
Given the instance person and field age, the correct syntax is person.age.
Final Answer:
person.age -> Option D
Quick Check:
Use dot (.) to access fields [OK]
Hint: Use dot (.) to access struct fields quickly [OK]
Common Mistakes:
Using arrow (->) like in C/C++
Trying to access fields like dictionary keys
Using double colon (::) which is for paths
2. Which of the following is the correct way to define a struct and access its field color in Rust?
easy
A. struct Car { color: String } let my_car = Car { color: "red" }; println!("{}", my_car["color"]);
B. struct Car { color: String } let my_car = Car { color: "red" }; println!("{}", my_car->color);
C. struct Car { color: String } let my_car = Car { color: String::from("red") }; println!("{}", my_car.color);
D. struct Car { color: String } let my_car = Car { color: String::from("red") }; println!("{}", Car.color);
Solution
Step 1: Check struct definition and instance creation
The struct Car is correctly defined with a field color of type String. The instance my_car is created with color: String::from("red"), which is correct.
Step 2: Verify field access syntax
The field is accessed using my_car.color, which is the correct way to access struct fields in Rust.
Final Answer:
struct Car { color: String } let my_car = Car { color: String::from("red") }; println!("{}", my_car.color); -> Option C
Quick Check:
Struct fields accessed with dot and proper String initialization [OK]
Hint: Use String::from for strings and dot for fields [OK]
Common Mistakes:
Using arrow (->) instead of dot (.)
Trying to access fields like dictionary keys
Using struct name instead of instance to access fields
3. What will be the output of this Rust code?
struct Point { x: i32, y: i32 }
fn main() {
let p = Point { x: 10, y: 20 };
println!("{}", p.x + p.y);
}
medium
A. 30
B. 1020
C. x y
D. Compilation error
Solution
Step 1: Understand struct and instance values
The struct Point has fields x and y with values 10 and 20 respectively.
Step 2: Calculate the sum of fields
The code prints the sum of p.x and p.y, which is 10 + 20 = 30.
Final Answer:
30 -> Option A
Quick Check:
Sum of fields x and y = 30 [OK]
Hint: Add numeric fields accessed by dot to get sum [OK]
Common Mistakes:
Concatenating numbers as strings
Using wrong field names
Expecting compilation error due to field access
4. Identify the error in this Rust code snippet:
struct User { name: String, age: u32 }
fn main() {
let user = User { name: String::from("Alice"), age: 30 };
println!("{}", user->name);
}
medium
A. Using arrow (->) instead of dot (.) to access field
B. Missing semicolon after struct definition
C. Incorrect type for age field
D. String::from used incorrectly
Solution
Step 1: Check field access syntax
The code uses user->name which is invalid in Rust. Rust uses dot (.) to access fields.
Step 2: Confirm other parts are correct
The struct definition, instance creation, and String::from usage are all correct.
Final Answer:
Using arrow (->) instead of dot (.) to access field -> Option A
Quick Check:
Use dot (.) not arrow (->) for fields [OK]
Hint: Remember Rust uses dot (.) for fields, not arrow (->) [OK]
Common Mistakes:
Confusing Rust with C/C++ pointer syntax
Thinking arrow works for references
Ignoring compiler error messages
5. Given this struct and code, what is the output?