Skip to content
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

fix(move_semantics2): fix line number comment #1484

Merged
merged 1 commit into from
Apr 22, 2023

Conversation

akgerber
Copy link
Contributor

Commit fef8314 added three lines of comments, which left the line numbers expected to stay unchanged mentioned on line 2 out of date.

Commit fef8314 added three lines of comments, which left the line
numbers expected to stay unchanged mentioned on line 2 out of date.
@akgerber
Copy link
Contributor Author

Also note that techniques 2 and 3 suggested in the hint do not produce the expected output per fef8314:

So, `vec0` is passed into the `fill_vec` function as an argument. In Rust,
when an argument is passed to a function and it's not explicitly returned,
you can't use the original variable anymore. We call this "moving" a variable.
Variables that are moved into a function (or block scope) and aren't explicitly
returned get "dropped" at the end of that function. This is also what happens here.
There's a few ways to fix this, try them all if you want:
1. Make another, separate version of the data that's in `vec0` and pass that
   to `fill_vec` instead.
2. Make `fill_vec` borrow its argument instead of taking ownership of it,
   and then copy the data within the function in order to return an owned
   `Vec<i32>`
3. Make `fill_vec` *mutably* borrow a reference to its argument (which will need to be
   mutable), modify it directly, then not return anything. Then you can get rid
   of `vec1` entirely -- note that this will change what gets printed by the
   first `println!

@shadows-withal shadows-withal merged commit a7eeaa3 into rust-lang:main Apr 22, 2023
@shadows-withal
Copy link
Member

@all-contributors please add @akgerber for content

@allcontributors
Copy link
Contributor

@shadows-withal

I've put up a pull request to add @akgerber! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants