Skip to content

[doubt]: Permissions in the unsafe-program-copying-vs-moving-out-of-a-collection example from the Rust book #130

Discussion options

You must be logged in to vote

Thanks for the question and sorry for the delayed response.

The difference between the first and second code examples you've provided is that the print statement extends the liveness of v. The Rust compiler is smart, so smart, that in your first image it says "hey, v is no longer used in this function so we can declare v dead after line 3." (If you're curious, the compiler uses something called a liveness analysis to figure this out.) As a result, the compiler will also remove permissions to that path.

In the second piece of code, where you inserted the print statement, the compiler determined that v was live for a little longer and that's why the read permissions (R) are retained. In an …

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Palanikannan1437
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants