cognitive-engineering-lab / rust-book

The Rust Programming Language: Experimental Edition
https://rust-book.cs.brown.edu
Other
504 stars 82 forks source link

Example of cloning to solve ownership issue may be confusing #132

Closed cagatay-y closed 5 months ago

cagatay-y commented 8 months ago

URL to the section(s) of the book with this problem: https://rust-book.cs.brown.edu/ch04-03-fixing-ownership-errors.html#fixing-an-unsafe-program-not-enough-permissions

Description of the problem: In the section that teaches copying as a possible solution to ownership errors, the copying is done via join. From the explanation

However, the clone copies every string in the input. We can avoid unnecessary copies by adding the suffix later:

it may be unclear to the beginner that the vector clone is not needed only because join already copies.

Suggested fix: Something like

However, the join already clones the strings inside the vector into a new string using an immutable reference to the vector. Therefore, cloning the vector beforehand is not necessary. We can add the suffix afterwards:

may explain what is happening more clearly.