Rust Struct Quiz
40 comprehensive questions on Rust's struct system, covering named fields, tuple structs, methods, associated functions, and field ownership — with 11 code examples demonstrating practical struct usage.
Question 1
What is a struct in Rust and why are they useful?
Question 2
How do you define a struct with named fields in Rust?
Question 3
How do you create an instance of a struct?
Question 4
What will this struct definition and instantiation create?
struct User {
username: String,
email: String,
sign_in_count: u64,
active: bool,
}
fn main() {
let user1 = User {
email: String::from("[email protected]"),
username: String::from("someusername123"),
active: true,
sign_in_count: 1,
};
}Question 5
How do you access fields of a struct instance?
Question 6
What is a tuple struct and how does it differ from a regular struct?
Question 7
How do you define a tuple struct?
Question 8
How do you access fields of a tuple struct?
Question 9
What will this tuple struct code output?
struct Color(i32, i32, i32);
fn main() {
let black = Color(0, 0, 0);
println!("Black color: ({}, {}, {})", black.0, black.1, black.2);
}Question 10
How do you implement methods on a struct?
Question 11
What is the difference between &self, &mut self, and self in method parameters?
Question 12
What will this method implementation do?
struct Rectangle {
width: u32,
height: u32,
}
impl Rectangle {
fn area(&self) -> u32 {
self.width * self.height
}
}
fn main() {
let rect = Rectangle { width: 30, height: 50 };
println!("Area: {}", rect.area());
}Question 13
How do you implement associated functions (static methods) on a struct?
Question 14
What is a common use case for associated functions?
Question 15
What will this associated function implementation create?
struct Rectangle {
width: u32,
height: u32,
}
impl Rectangle {
fn square(size: u32) -> Rectangle {
Rectangle { width: size, height: size }
}
}
fn main() {
let sq = Rectangle::square(3);
println!("Square: {}x{}", sq.width, sq.height);
}Question 16
How does field ownership work in structs when the struct is moved?
Question 17
What happens when you try to access a field of a moved struct?
Question 18
How do you modify struct fields when the struct is mutable?
Question 19
What will this mutable struct code do?
struct User {
email: String,
username: String,
active: bool,
}
fn main() {
let mut user1 = User {
email: String::from("[email protected]"),
username: String::from("someusername123"),
active: true,
};
user1.email = String::from("[email protected]");
println!("New email: {}", user1.email);
}Question 20
How do you create a struct with some fields from another struct instance?
Question 21
What will this struct update syntax create?
struct User {
username: String,
email: String,
sign_in_count: u64,
active: bool,
}
fn main() {
let user1 = User {
email: String::from("[email protected]"),
username: String::from("someusername123"),
active: true,
sign_in_count: 1,
};
let user2 = User {
email: String::from("[email protected]"),
..user1
};
println!("User2 active: {}", user2.active);
}Question 22
How do you destructure a struct in a let binding?
Question 23
What will this struct destructuring do?
struct Point {
x: i32,
y: i32,
}
fn main() {
let p = Point { x: 0, y: 7 };
let Point { x, y } = p;
println!("x: {}, y: {}", x, y);
}Question 24
How do you implement the Debug trait for a struct to enable println! formatting?
Question 25
What is the difference between tuple structs and regular structs?
Question 26
When should you use tuple structs instead of named field structs?
Question 27
How do you handle structs with many fields in function parameters?
Question 28
What is the unit-like struct and when is it useful?
Question 29
How do you implement the Display trait for custom formatting?
Question 30
What will this Display implementation do?
use std::fmt;
struct Point {
x: i32,
y: i32,
}
impl fmt::Display for Point {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({}, {})", self.x, self.y)
}
}
fn main() {
let p = Point { x: 3, y: 4 };
println!("{}", p);
}Question 31
How do you create methods that modify struct fields?
Question 32
What is the difference between methods and associated functions?
Question 33
How do you implement getter and setter methods for struct fields?
Question 34
What happens to struct field ownership when implementing Drop trait?
Question 35
How do you handle structs containing references?
Question 36
What is struct field visibility and how does it work?
Question 37
How do you create a struct with borrowed fields?
Question 38
What is the builder pattern and how is it implemented with structs?
Question 39
How do you implement Clone trait for a struct?
Question 40
In a complex application managing user profiles with multiple optional fields, how would you design the user struct to handle missing data gracefully?
