Warning
This post was published 43 days ago. The information described in this article may have changed.
In the last days, I sometimes had a look into the book from the Brown University, mostly for their quizzes -- some are really quite difficult.
I just saw the following section in Unsafe Rust - The Rust Programming Language
To perform unsafe operations in the body of an unsafe function, you still need to use an
unsafe
block, just as within a regular function, and the compiler will warn you if you forget. This helps to keepunsafe
blocks as small as possible, as unsafe operations may not be needed across the whole function body.
I thought that must be an mistake, but we have now the same in the original book.
I am quite sure that I read that unsafe blocks are not required in unsafe functions 18 months ago when I started seriously learning Rust -- but I might confuse it with another book, perhaps the one from Jim Blandy.
So what is the current state? Have unsafe blocks been necessary in unsafe functions in the very early days of Rust? Or are they again required with the latest compiler? Or is the official book just wrong? (Of course I know that I need an unsafe block to call an unsafe function.). Or, is it just good style to use unsafe blocks even inside of unsafe functions, even when it is not really required?
6 posts - 6 participants
🏷️ rust_feed