expression
Prefer checked access before unsafe operations
A safe interface is the default choice. Here a failed index lookup returns None rather than performing unchecked memory access. Unsafe code is justified only when a necessary capability or measured constraint cannot be addressed adequately by a safe interface. Expected stdout (not executed): checked access: 20; missing: None Common mistake: Assuming unsafe indexing is automatically faster without measuring.
Formal Syntax & Structure
rust syntaxrust
slice.get(index)Working Example
Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() { let values = [10, 20, 30]; assert_eq!(values.get(1), Some(&20)); assert_eq!(values.get(3), None); println!("checked access: 20; missing: None");}