Accessing a struct

In order to access a struct variable, we will need to create a variable that can access it:

let mut room = Room { is_upstairs: true, 
number_of_doors: 1, wood_or_carpet: true, carpet_color: "Red",
room_name: "Bedroom 1", has_wardrobe: true };
The code for this section is in the 07/simplestruct folder in the supporting code bundle of this book.

We have not defined all of the variables within that structure, which, for now, is fine, as they still need to be defined before the code will compile. The variable is mutable since we want to change its contents later.

To access one of the members of the struct, we will use the dot notation. In this case, we can have the following:

println!("Bedroom {} has {} door", room.room_name, 
room.number_of_door);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset