Size of unsafe blocks
โ Rust ๐ 2025-11-18 ๐ค surdeus ๐๏ธ 4Continuing this discussion from quote of the week; how does everyone prefer to handle the scope of unsafe blocks?
Personally, I enable the unsafe_code lint (at the warn level), and add #![expect(unsafe_code, reason = "..")] at the top of modules with any unsafe (and I keep unsafe blocks small). Iโm also a fan of relatively verbose safety comments, and using small unsafe blocks is an easy way to require that each unsafe action gets its own explanation of why its safety invariants are upheld. I suppose itโs still possible for someone to modify other safe code in the module without bothering to read all the comments, but if that happened, my solution would probably be to add more comments to the safe code as well.
2 posts - 2 participants
๐ท๏ธ Rust_feed