-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The book should state _why_ ownership is not wanted in the example #3961
Comments
It's simple as: A reference I suggest you to re-read the following carefully:
|
I see why you stumbled over this! However, the book answers your question if you keep reading: we don’t want ownership here because we want to get an answer about this string without moving it away from the original function, so that we can continue using the string in the original function: let s = String::from("hello world");
let word = first_word(&s);
// other things with `s`, as shown in the rest of the section Remember from 4.01: What is Ownership? that if we moved it into the fn first_word(s: String) {
// if we don’t do anything else, `s` gets dropped when we return!
} The only way to avoid that would be to also return it from that function. You could imagine writing a signature like All that to say: the rest of the example shows why we don’t need or want ownership of the string. We don’t want to try to stop and explain all of this at the point where the statement is made, because it becomes clear by continuing to read the rest of the section. |
I mean stating the reason right there would make it much more understandable and not harm anyone, right? |
URL to the section(s) of the book with this problem:
https://doc.rust-lang.org/book/ch04-03-slices.html
Description of the problem:
After the first code block, the following text is written:
I don't understand why in that case ownership is not required / wanted
Suggested fix:
Add a short explanation
The text was updated successfully, but these errors were encountered: