huiali/rust-skills/rust-unsafe
Claude Code and agent skill by huiali/rust-skills
Trust tier 2
- community
Source check pending
Checking that the SKILL.md file is still in the repository.
Unsafe code and FFI expert covering raw pointers (*mut, *const), FFI patterns, transmute, union, #[repr(C)], SAFETY comments, soundness rules, and undefined behavior prevention.
Install
npx vskill@latest install huiali/rust-skills/rust-unsafeSKILL.md
When Unsafe is Justified
| Use Case | Example | Justified? | |
|---|---|---|---|
| FFI calls to C | extern "C" { fn libc_malloc(size: usize) -> *mut c_void; } | ✅ Yes | |
| Low-level abstractions | Internal implementation of Vec, Arc | ✅ Yes | |
| Performance optimization (measured) | Hot path with proven bottleneck | ⚠️ Verify first | |
| Escaping borrow checker | Don't know why you need it | ❌ No |
SAFETY Comment Requirements
Every unsafe block must include a SAFETY comment:
Solution Patterns
Pattern 1: FFI with Safe Wrapper
Pattern 2: Raw Pointer with Validation
Pattern 3: Uninitialized Memory
Pattern 4: Repr(C) for FFI
47 Unsafe Rules Reference
General Principles (3 rules)
| Rule | Description | |
|---|---|---|
| G-01 | Don't use unsafe to escape compiler safety checks | |
| G-02 | Don't blindly use unsafe for performance | |
| G-03 | Don't create "Unsafe" aliases for types/methods |
Memory Layout (6 rules)
| Rule | Description | |
|---|---|---|
| M-01 | Choose appropriate memory layout for struct/tuple/enum | |
| M-02 | Don't modify memory variables of other processes | |
| M-03 | Don't let String/Vec auto-deallocate memory from other processes | |
| M-04 | Prefer reentrant versions of C-API or syscalls | |
| M-05 | Use third-party crates for bit fields | |
| M-06 | Use MaybeUninit<T> for uninitialized memory |
Raw Pointers (6 rules)
| Rule | Description | |
|---|---|---|
| P-01 | Don't share raw pointers across threads | |
| P-02 | Prefer NonNull<T> over *mut T | |
| P-03 | Use PhantomData<T> to mark variance and ownership | |
| P-04 | Don't dereference pointers cast to misaligned types | |
| P-05 | Don't manually convert immutable pointers to mutable | |
| P-06 | Use ptr::cast instead of as for pointer casts |
Unions (2 rules)
| Rule | Description | |
|---|---|---|
| U-01 | Avoid unions except for C interop | |
| U-02 | Don't use union variants with different lifetimes |
FFI (18 rules)
| Rule | Description | |
|---|---|---|
| F-01 | Avoid passing strings directly to C | |
| F-02 | Carefully read std::ffi types documentation | |
| F-03 | Implement Drop for wrapped C pointers | |
| F-04 | Handle panics across FFI boundaries | |
| F-05 | Use portable type aliases from std or libc | |
| F-06 | Ensure C-ABI string compatibility | |
| F-07 | Don't implement Drop for types passed to extern code | |
| F-08 | Handle errors properly in FFI | |
| F-09 | Use references instead of raw pointers in safe wrappers | |
| F-10 | Exported functions must be thread-safe | |
| F-11 | Be careful with references to repr(packed) fields | |
| F-12 | Document invariant assumptions for C parameters | |
| F-13 | Ensure consistent data layout for custom types | |
| F-14 | FFI types should have stable layout | |
| F-15 | Validate robustness of external values | |
| F-16 | Separate data and code for C closures | |
| F-17 | Use opaque types instead of c_void | |
| F-18 | Avoid passing trait objects to C |
Safe Abstractions (11 rules)
| Rule | Description | |
|---|---|---|
| S-01 | Be aware of memory safety issues from panics | |
| S-02 | Unsafe code authors must verify safety invariants | |
| S-03 | Don't expose uninitialized memory in public APIs | |
| S-04 | Avoid double-free from panics | |
| S-05 | Consider safety when manually implementing Auto Traits | |
| S-06 | Don't expose raw pointers in public APIs | |
| S-07 | Provide safe alternatives for performance | |
| S-08 | Returning mutable reference from immutable parameter is wrong | |
| S-09 | Add SAFETY comment before each unsafe block | |
| S-10 | Add Safety section to public unsafe function docs | |
| S-11 | Use assert! instead of debug_assert! in unsafe functions |
I/O Safety (1 rule)
| Rule | Description | |
|---|---|---|
| I-01 | Ensure I/O safety when using raw handles |
Workflow
Step 1: Question the Need
Step 2: Write SAFETY Comments
Step 3: Validate with Tools
Step 4: Build Safe Wrappers
Common Errors and Fixes
| Error | Fix | |
|---|---|---|
| Null pointer dereference | Check for null before dereferencing | |
| Use after free | Ensure lifetime validity | |
| Data race | Add synchronization | |
| Alignment violation | Use #[repr(C)], check alignment | |
| Invalid bit pattern | Use MaybeUninit | |
| Missing SAFETY comment | Add comment |
Deprecated Patterns
| Deprecated | Modern Alternative | |
|---|---|---|
mem::uninitialized() | MaybeUninit<T> | |
mem::zeroed() (for reference types) | MaybeUninit<T> | |
| Raw pointer arithmetic | NonNull<T>, ptr::add | |
CString::new().unwrap().as_ptr() | Store CString first | |
static mut | AtomicT or Mutex | |
| Manual extern declarations | bindgen |
FFI Tools
| Direction | Tool | |
|---|---|---|
| C → Rust | bindgen | |
| Rust → C | cbindgen | |
| Python | PyO3 | |
| Node.js | napi-rs | |
| C++ | cxx |
Review Checklist
When reviewing unsafe code:
- [ ] Unsafe usage is justified (FFI, low-level abstraction, measured perf)
- [ ] Every unsafe block has SAFETY comment
- [ ] Public unsafe functions document Safety requirements
- [ ] Raw pointers are validated before dereferencing
- [ ] No raw pointer sharing across threads without sync
- [ ] FFI boundaries properly handle panics
- [ ] Memory layout explicitly specified for FFI types
- [ ] Uninitialized memory uses
MaybeUninit - [ ] Safe public API wraps unsafe internals
- [ ] Tested with Miri for undefined behavior
Verification Commands
Common Pitfalls
1. Dangling Pointers
Symptom: Use-after-free, segfault
2. Uninitialized Memory
Symptom: Undefined behavior, random values
3. Invalid Repr
Symptom: FFI crashes, data corruption
Related Skills
- rust-ownership - Lifetime and borrowing fundamentals
- rust-ffi - Advanced FFI patterns
- rust-performance - When unsafe optimization is justified
- rust-coding - SAFETY comment conventions
- rust-concurrency - Thread-safe unsafe patterns
Localized Reference
- Chinese version: SKILL_ZH.md - 完整中文版本,包含所有内容
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewNot runA model review is scheduled for the next rescan.
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/rust-unsafe/SKILL.md
Popularity
- Repository stars
- 47
- Repository forks
- 8
- 7-day trend score
- 17
Stars and forks are for the whole GitHub repository, not this skill alone. Refreshed 11 Apr 2026, 7 months ago.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/huiali/rust-skills/rust-unsafe)