Which of the following best explains why structs are used in Rust?
Think about how you keep related information together in real life, like a contact card.
Structs let you bundle different pieces of data into one named type, making code organized and easier to understand.
What is the output of this Rust code?
struct Point {
x: i32,
y: i32,
}
fn main() {
let p = Point { x: 3, y: 4 };
println!("{}", p.x);
}Look at which field is printed inside println!.
The code prints the value of p.x, which is 3.
What error does this Rust code produce?
struct Person {
name: String,
age: u8
}
fn main() {
let p = Person { name: String::from("Alice"), age: 30 };
}Look carefully at how the name field is assigned.
The name field expects a String, but a string slice &str is given. Rust requires explicit conversion.
Which option contains the syntax error in defining a struct in Rust?
Remember how fields are separated inside structs.
Struct fields must be separated by commas, not semicolons.
Given this Rust code, how many fields does the book instance have?
struct Book {
title: String,
author: String,
pages: u32,
available: bool
}
fn main() {
let book = Book {
title: String::from("Rust Guide"),
author: String::from("Jane Doe"),
pages: 250,
available: true
};
}Count the fields declared inside the struct and initialized in the instance.
The struct Book has four fields: title, author, pages, and available.
