rust-unsafe-guide

Guides writing, encapsulating, and verifying unsafe Rust code with Miri.

Updated Mar 29, 2026
One-click install
npx skills add https://github.com/luckyegg168/rust-skill --skill rust-unsafe-guide-luckyegg168
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: rust-unsafe-guide
Source: https://github.com/luckyegg168/rust-skill/tree/main/rust-skills/rust-unsafe-guide
Command: npx skills add https://github.com/luckyegg168/rust-skill --skill rust-unsafe-guide-luckyegg168

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Writing unsafe Rust without introducing undefined behavior is difficult: raw pointer dereferences, transmute misuse, data races, and uninitialized memory reads can silently corrupt programs. This Skill provides structured guidance on the five unsafe superpowers, safe abstraction patterns, and Miri-based verification so AI agents and developers produce correct unsafe code. ## Core Features & Use Cases - Unsafe Operation Reference: Covers all five unsafe capabilities (raw pointer dereference, unsafe fn calls, mutable static access, unsafe trait impls, union field access) with risk levels and typical scenarios. - Safe Encapsulation Patterns: Shows how to minimize unsafe scope, write SAFETY comments, and wrap unsafe internals behind safe public APIs, including a full MyVec implementation with alloc/dealloc/grow logic. - UB Detection with Miri: Documents Miri installation, execution, detectable issues (use-after-free, data races, Stacked Borrows violations), and its limitations, plus a 10-item common UB checklist. - Use Case: When implementing a custom data structure or FFI binding in Rust, load this Skill to get correct Pin-based self-referential struct patterns, transmute alternatives (bytemuck, zerocopy), and a review checklist ensuring every unsafe block passes cargo +nightly miri test. ## Quick Start Ask the AI to review or write unsafe Rust code using the rust-unsafe-guide skill, for example to implement a custom Vec with proper SAFETY comments and Miri verification.

Frequently Asked Questions about rust-unsafe-guide

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write unsafe Rust code safely?▼

Minimize each unsafe block to only the necessary operation, add a // SAFETY: comment explaining why it is sound, and expose a safe public API that validates preconditions. Verify the result with cargo +nightly miri test to detect undefined behavior.

How to detect undefined behavior in Rust with Miri?▼

Install Miri with rustup +nightly component add miri, then run cargo +nightly miri test. It detects out-of-bounds access, use-after-free, data races, uninitialized reads, and Stacked Borrows violations, though it runs 10-100x slower than native execution.

What are the five unsafe superpowers in Rust?▼

The five unsafe operations are dereferencing raw pointers, calling unsafe functions, accessing or modifying mutable statics, implementing unsafe traits like Send and Sync, and accessing union fields. Each carries different risk levels and typical use cases such as FFI or custom collections.

When should I use transmute in Rust?▼

Use transmute only as a last resort for same-layout bit reinterpretation. Prefer safer alternatives in order: as casts for primitives, From/Into traits, then bytemuck or zerocopy for safe bit conversions. Transmuting invalid bit patterns into enums or bool is undefined behavior.

Why does Rust require Pin for self-referential structs?▼

Self-referential structs hold pointers to their own fields, so moving them invalidates those pointers. Pin guarantees the memory location stays fixed, and PhantomPinned prevents auto-Unpin, making the self-references sound.

What are the limitations of Miri for testing unsafe code?▼

Miri cannot execute real system calls, has limited file I/O simulation, runs 10-100x slower than native code, and does not support all FFI without shims. It also only finds UB on executed code paths, not all possible undefined behavior.