From the course: Rust Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Struct update syntax

Struct update syntax - Rust Tutorial

From the course: Rust Essential Training

Start my 1-month free trial

Struct update syntax

- [Teacher] We only define a struct once, which is done here on line two at the top of the program. Once we've done that we can use that definition to instantiate as many separate instances of the struct as we need. Let's expand our fleet of spacecraft by creating a second shuttle named vehicle two, and we'll make its name Discovery. Now, instead of explicitly defining the rest of the fields let's say we want our second vehicle to be initialized using the same values as the first vehicle. For that we can use struct update syntax, which allows us to copy values from an existing instance. It's written as .. and then the name of the existing instance to copy values from. This tells Rust that any fields that are not explicitly set should have the same value as the first vehicle. To see that let's copy and paste the print statement to look at the first vehicle, and modify it to print the second vehicle, and we'll also delete…

Contents