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 unsafe as a solution for ownership error is not valid #133

Closed cagatay-y closed 8 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?search=#fixing-a-safe-program-mutating-different-array-elements

Description of the problem: The book explains that split_first_mut works using unsafe, which as far as I understand is not the case (since 2020 based on git blame).

Suggested fix: This explanation cannot simply be removed, since it is used as a segue to explain the occasional necessity of unsafe blocks. I don't know what would be better example to make the case for unsafe blocks in this case.

willcrichton commented 8 months ago

Ah great catch! I had originally written this using split_at_mut (which does actually use unsafe), and then thought "hmm well it would be simpler to just use split_first_mut. But of course, that's a special case that reduces to pattern matching :| I will revert the section to use split_at_mut.