cognitive-engineering-lab / rust-book

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

Free before use? #45

Closed cmcaine closed 1 year ago

cmcaine commented 1 year ago

This says that Rust frees and then allocates new larger memory. Perhaps it should say that Rust allocates a larger chunk of memory, writes "Ferris Jr." and then frees the first allocation?

https://github.com/cognitive-engineering-lab/rust-book/blob/d2af51055890d1d3611d3714f9b5a6a087f75dfe/src/ch04-01-what-is-ownership.md?plain=1#L276

Besides, isn't the exact behaviour an implementation detail of String?

willcrichton commented 1 year ago

Thanks for the issue, I've clarified the order of operations.

This behavior is an implementation detail of string, but it is relevant to understanding memory safety.