In your example, the following line is creating a new
String every time the function gets called:
let mut what = "the".to_string();
The important thing to keep in mind is that when you define a string literal in your source code (such as
"the"), its type is
&'static str - in other words, a reference to the static string that’s stored in your program. You can’t return ownership of
"the" directly, because you don’t own it! So when you call
to_string(), you’re effectively copying the contents of that string into a new one that you do own.
This example might make that concept a bit clearer:
// The above line of code is equivalent to this, with explicit type annotations added:
let the: &'static str = "the";
let mut what: String = the.to_string();
Since you’re returning this new string directly rather than holding on to it somewhere and returning a reference, the caller gets ownership of it. This part doesn’t do any copying.
So to answer your question,
d are all separate strings, but they’re all owned by
main. If you looked at a memory map,
"the" and all four of your return values would most likely have separate locations. Does that make sense?