expression
Document caller obligations of an unsafe function
An unsafe function declares an obligation that the caller must establish. Its implementation still needs to justify each unsafe operation. In edition 2024 unsafe_op_in_unsafe_fn warns by default; this fixture strengthens it to deny. Expected stdout (not executed): 17 Common mistake: Treating unsafe fn as permission to omit proof of its internal operations.
Formal Syntax & Structure
rust syntaxrust
unsafe fn read(p: *const i32) -> i32Working Example
Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() { /// # Safety /// p must be valid and aligned for reading an initialized i32, /// with no conflicting accesses for the duration of the read. unsafe fn read(p: *const i32) -> i32 { // SAFETY: guaranteed by the caller's documented contract. unsafe { *p } } let value = 17; // SAFETY: p is derived from a live shared reference to value. let result = unsafe { read(&value) }; println!("{result}");}