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 lifetime annotations

Struct lifetime annotations - Rust Tutorial

From the course: Rust Essential Training

Start my 1-month free trial

Struct lifetime annotations

- Instructor] When struct stores data using the string data type it has ownership of that string. So when the struct goes out of scope that string data will also be dropped and removed from the heat. Consider the simplified shuttle structure shown here which stores the shuttle's name as a string. It has a single method named send transmission on line six which accepts an input slice for the message to transmit, prints that message and then returns a reference to the shuttle's name. If I run this program line 17 calls that method to transmit a greeting from orbit and then we see from the return string slice that the sender was endeavor. Now let's try modifying that struck to store the name as a string slice instead of a full string. We'll change the struct definition. And since we're now using a borrow reference we no longer need to use the borrow operator for the return expression on line eight. We also need to change line…

Contents