解引用裸指针
创建指针是安全的操作,但解引用指针需要使用 unsafe
方法:
fn main() { let mut num = 5; let r1 = &mut num as *mut i32; let r2 = r1 as *const i32; // Safe because r1 and r2 were obtained from references and so are // guaranteed to be non-null and properly aligned, the objects underlying // the references from which they were obtained are live throughout the // whole unsafe block, and they are not accessed either through the // references or concurrently through any other pointers. unsafe { println!("r1 is: {}", *r1); *r1 = 10; println!("r2 is: {}", *r2); } }
我们建议(而且 Android Rust 样式指南要求)为每个 unsafe
代码块编写一条注释, 说明该代码块中的代码如何满足其所执行的不安全操作的 安全要求。
对于指针解除引用,这意味着指针必须为 valid,即:
- 指针必须为非 null。
- 指针必须是 dereferenceable(在单个已分配对象的边界内)。
- 对象不得已取消分配。
- 不得并发访问相同位置。
- 如果通过转换引用类型来获取指针,则底层对象必须处于活跃状态, 而且不得使用任何引用来访问内存。
在大多数情况下,指针还必须正确对齐。