Owned Strings
Explore how Rust handles dynamic strings with the owned String type, which uses heap memory to store data created at runtime. Understand the limitations of string slices, learn to fix concatenation errors using to_owned, and grasp how Rust's memory model supports efficient string management.
We'll cover the following...
There’s a big restriction on program memory versus stack; once the compiler creates your executable, the program memory is frozen. When your executable is running, it cannot change data in program memory or add new things there.
Let’s give a simple program we’d like to make work somehow. I’d like to create a new string that combines my first and last name together. Naively, I would try to write something like this:
This program doesn’t work. We’ll look at the compiler error in a bit to get help fixing it. But first, let me ask you a basic question, where should the full_name value live in memory? We know it can’t live in program memory, since this string is going to be created while ...