Why this matters
Explicit contracts prevent undefined behavior and misuse.
When a function is unsafe, may panic, or returns Result, include sections # Safety, # Panics, and/or error conditions in the docs.
Explicit contracts prevent undefined behavior and misuse.
Side-by-side examples engineers can pattern-match during review.
/// Write to ptr.
pub unsafe fn write(ptr: *mut u8, v: u8) { ... }/// Write a value to a raw pointer.
///
/// # Safety
/// Caller must ensure ptr is valid for writes.
///
/// # Panics
/// Panics if alignment is incorrect.
pub unsafe fn write(ptr: *mut u8, v: u8) { ... }/// # Panics
/// Panics if index out of bounds./// Might panicFrom the same buckets as this rule.
Check if loops use equality operators (== or !=) in termination conditions. These can lead to infinite loops if the condition is never met exactly. Instead, use relational operators like < or > for safer loop termination.