maverick-rust-unsafe

get2knowio 的头像作者 get2knowio

Rust 不安全代码、FFI 和安全性不变量。

📖 中文导读:Rust 不安全代码、FFI 和安全性不变量。 以下正文为该技能的原始 SKILL.md 内容(英文),上方安装命令可直接使用。

name: maverick-rust-unsafe description: Rust unsafe code, FFI, and safety invariants version: 1.0.0 triggers: - unsafe - "unsafe {" - "unsafe fn" - raw pointer - "*const" - "*mut" - transmute - FFI - extern

Rust Unsafe Skill

When to Use Unsafe

  • FFI with C libraries
  • Raw pointers for performance
  • Type transmutation (rare, dangerous)
  • Unsafe trait implementations

Safety Documentation

/// SAFETY: The slice is guaranteed to be non-empty by the caller.
/// This is enforced by the type system via NonEmptySlice.
unsafe fn get_first_unchecked(slice: &[i32]) -> i32 {
    debug_assert!(!slice.is_empty());
    *slice.get_unchecked(0)
}

Avoid Unsafe When Possible

// BAD - unjustified unsafe
unsafe fn get_first(slice: &[i32]) -> i32 {
    *slice.get_unchecked(0)
}

// GOOD - safe alternative
fn get_first(slice: &[i32]) -> Option<&i32> {
    slice.first()
}

Review Severity

  • CRITICAL: Unsafe without justification, potential undefined behavior
  • MAJOR: Missing safety documentation, unsafe could be safe
  • MINOR: Debug assertions missing in unsafe code