From the course: First Look: Rust

Unlock the full course today

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

Ownership: Memory allocation

Ownership: Memory allocation - Rust Tutorial

From the course: First Look: Rust

Start my 1-month free trial

Ownership: Memory allocation

- [Instructor] To illustrate the rules of ownership we need a data type that is more complex than the ones we have covered in the previous chapter. Let's create a new project and call it memory. And let me CD into the project. Let's open up the Rust file that Cargo has created for us and remove the standard code. So here we'll use the string as the example here and concentrate on the parts of a string that relate to ownership. These aspects also apply to other complex data types provided by the standard library and that you create. Let's create a string, S. Let mute S equals string, which is a key word here, and double colon. The double colon is an operator that allows us to namespace this particular from function under the string type. And then we say from, and then parse the string that we want here. This type is allocated on the heap and as such is able to store an amount of text that is unknown to us at compile time. This kind of string can be mutated using the function push_str…

Contents