第17章 不安全性

不安全性 — The Rust Reference

译文 · 基于 The Rust Reference

原文链接: https://doc.rust-lang.org/reference/unsafety.html

r[safety]

不安全性

r[safety.intro] 不安全操作是那些可能违反 Rust 静态语义所保证的内存安全的操作。

r[safety.unsafe-ops] 下列语言级特性不能在 Rust 的安全子集中使用:

r[safety.unsafe-deref]

r[safety.unsafe-static]

r[safety.unsafe-union-access]

  • 访问 union 的字段(赋值除外)。

r[safety.unsafe-call]

  • 调用不安全函数。

r[safety.unsafe-target-feature-call]

  • 从未启用相同特性的 target_feature 属性的函数中,调用带有 [target_feature][attributes.codegen.target_feature] 标记的安全函数(参见 [attributes.codegen.target_feature.safety-restrictions])。

r[safety.unsafe-impl]

r[safety.unsafe-extern]

r[safety.unsafe-attribute]


  1. 在 2024 edition 之前,允许在没有 unsafe 的情况下声明 extern 块。 ↩︎


01-unsafe 关键字

unsafe 关键字 — The Rust Reference

02-视为未定义行为

视为未定义行为 — The Rust Reference

03-不视为不安全的行为

不视为不安全的行为 — The Rust Reference

最后修改 August 21, 2026: 更新 (76fc81a2e)