Consider this Rust code defining a struct and printing a field:
struct Person {
name: String,
age: u8,
}
fn main() {
let p = Person { name: String::from("Alice"), age: 30 };
println!("{} is {} years old.", p.name, p.age);
}What will this program print?
struct Person {
name: String,
age: u8,
}
fn main() {
let p = Person { name: String::from("Alice"), age: 30 };
println!("{} is {} years old.", p.name, p.age);
}Look at how the println! macro uses the fields.
The program creates a Person struct with name "Alice" and age 30. The println! macro prints the name and age in order, so the output is "Alice is 30 years old.".
Examine this Rust code:
struct Point {
x: i32,
y: i32,
}
fn main() {
let p = Point { x: 5 };
println!("({}, {})", p.x, p.y);
}What error will the compiler show?
struct Point {
x: i32,
y: i32,
}
fn main() {
let p = Point { x: 5 };
println!("({}, {})", p.x, p.y);
}Check if all fields are initialized when creating a struct instance.
Rust requires all fields to be initialized when creating a struct instance. Here, `y` is missing, so the compiler errors with missing field `y`.
Look at this Rust code:
struct Rectangle {
width: u32,
height: u32,
}
fn main() {
let rect = Rectangle { width: 30, height: 50 };
println!("The area is {}", rect.area());
}
impl Rectangle {
fn area(&self) -> u32 {
self.width * self.height
}
}Why does this code fail to compile?
struct Rectangle {
width: u32,
height: u32,
}
fn main() {
let rect = Rectangle { width: 30, height: 50 };
println!("The area is {}", rect.area());
}
impl Rectangle {
fn area(&self) -> u32 {
self.width * self.height
}
}Check if methods can be called on immutable instances.
The method `area` is defined in an `impl` block for `Rectangle`. It takes `&self` (immutable reference), so it can be called on an immutable instance. The code compiles and prints "The area is 1500".
Which of these options correctly defines a tuple struct named Color with three u8 fields?
Tuple structs use parentheses with types inside.
Option C correctly defines a tuple struct with three u8 fields. Option C uses braces but no field names, which is invalid. Option C uses an equal sign which is invalid syntax. Option C misses commas between types.
Given this Rust code:
struct User {
username: String,
email: String,
active: bool,
sign_in_count: u64,
}
fn main() {
let mut user1 = User {
email: String::from("user@example.com"),
username: String::from("user123"),
active: true,
sign_in_count: 1,
};
let user2 = User {
email: String::from("new@example.com"),
..user1
};
println!("User2 username: {}", user2.username);
}How many fields does user2 have and what is its username value?
struct User {
username: String,
email: String,
active: bool,
sign_in_count: u64,
}
fn main() {
let mut user1 = User {
email: String::from("user@example.com"),
username: String::from("user123"),
active: true,
sign_in_count: 1,
};
let user2 = User {
email: String::from("new@example.com"),
..user1
};
println!("User2 username: {}", user2.username);
}Remember that struct update syntax copies remaining fields.
The struct User has 4 fields. The update syntax copies all fields from user1 except email which is replaced. So user2 has 4 fields and username remains "user123".
