Complete the code to define a struct named Person.
struct [1] {
name: String,
age: u32,
}The struct name should start with a capital letter by convention. Here, Person is the correct name.
Complete the code to create an instance of the struct Person.
let person = Person {
name: String::from("Alice"),
[1]: 30,
};The field for age in the struct is named age, so we must use age when creating an instance.
Fix the error in accessing the name field of the person instance.
println!("Name: {}", person.[1]);
To access a field in a struct instance, use the exact field name in lowercase, here name.
Fill both blanks to define a struct and create an instance with a field value.
struct [1] { [2]: i32, } let data = [1] { value: 10 };
The struct is named Data and has a field value. The instance is created using the same struct name.
Fill all three blanks to create a struct, instantiate it, and access its field.
struct [1] { [2]: String, } let item = [1] { name: String::from("Book") }; println!("Item: {}", item.[2]);
The struct is named Product with a field name. We create an instance and access the name field.
