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.

Moving, cloning, and copying data

Moving, cloning, and copying data - Rust Tutorial

From the course: Rust Essential Training

Start my 1-month free trial

Moving, cloning, and copying data

- [Instructor] The key restriction that resources can only have one owner at any given time may sound simple but it can complicate things because different types of data work differently When we try to use them with multiple variables. Consider this string variable on line three named inner planet. I've named it inner planet because it's declared inside the scope of the curly braces between lines two to five. Inner planet will become valid starting on line three when it takes ownership of the newly initialized string which is allocated on the heap. Then at line five, when inner planet goes out of scope it will be dropped. And the heap memory used to store that string will be free. That means by the print macro on line six inner planet will be out of scope and no longer valid. If I try to run this code, sure enough it fails at line six because inner planet no longer exists. This example may seem familiar because it's…

Contents